python写utf8文件
来源:互联网 发布:知无涯者书 编辑:程序博客网 时间:2024/05/29 05:02
【1】使用默认open方式,
需要自己确保需要的编码方式,如下例子中xxx.json为utf8格式,读入后为unicode,需要转为utf8再写文件
name='xxx.json'f=open(name,'r')of=open(name+'.txt','w')for line in f:dictdata=json.loads(line)out=json.dumps(dictdata,indent=4<span style="color:#3333ff;">, ensure_ascii=False</span>).encode('utf8')of.write(out+'\n')of.close()对于如上json格式化的例子来说,ensure_ascii也是可以不要的。
【2】使用codecs库
这时候需要指定的是读入或写出的文件的编码。
name='xxx.json'f=codecs.open(name,'r',encoding='utf8')of=codecs.open(name+'.txt','w', encoding='utf8')for line in f:dictdata=json.loads(line)out=json.dumps(dictdata,indent=4, ensure_ascii=False)of.write(out+'\n')of.close()这种方式ensure_ascii貌似一定需要的了。
0 0
- python写utf8文件
- python写的批量将c/c++代码文件转换为utf8编码脚本
- Python文件编码---gbk?OR utf8?
- VBA写txt文件--vba写RF文件--vbs写文本文件不同编码--vba写UTF8
- C#写UTF8文件时指定是否含BOM头
- Python读取文件,检测文件编码及去除UTF8 BOM
- python 写文件
- python 写xml文件
- python写文件
- python 写文件换行
- python写文件
- Python 写文件
- python 写文件
- python写文件出错
- python写csv文件
- Python写Excel文件
- python写csv文件
- python 写 yml文件
- mac 下 swoole 环境搭建
- grep命令详解
- leetcode 56: Merge Intervals
- 利用for循环和条件运算符输出一个数组中最大和最小值并分别输出下标
- dubbo总结(7)dubbo注册中心集群
- python写utf8文件
- java.容器
- CSS+DIV:实现炫酷网页样式与布局
- 事务的隔离级别解析
- spring+hibernate注解配置实例
- Mysql 存储过程中应用事务
- hdu 5296 Annoying problem(LCA)
- HDU_5327_Olympiad
- ITOO之底层搭建(一)——缺少jar