#用Python直接写UTF-8文本文件
2015-09-03 08:07
615 查看
当我们这样建立文件时
直接结果应该是类似
要直接写 utf-8 文件怎么办呢?
f = file('x1.txt', 'w') f.write(u'中文') f.colse()
直接结果应该是类似
f.write(u'中文') UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-16: ordinal not in range(128)
要直接写 utf-8 文件怎么办呢?
import codecs f = codecs.open("pru_uni.txt", "w", "utf-8") txt = unicode("campeón\n", "utf-8") f.write(txt) f.write(u'中文\n') f.close()
相关文章推荐
- Python开发环境搭建
- Python中的元类(metaclass)
- python 字符串转换,mysql插入带引号字符串,获取html纯文字
- Python资料合集
- Python-BlogServer博客-Django博客效果
- Python--Cmd窗口运行Python时提示Fatal Python error: Py_Initialize: can't initialize sys standard streams LookupError: unknown encoding: cp65001
- Python线程的两种创建方法
- python 学习备忘9.2
- Python的Threading模块的Thread对象的join方法
- Python视频教程
- Building Python 2.7.10 with Visual Studio 2010 or 2015 - Google Chrome
- python webdriver API
- "Python"学习笔记(一)
- python 实现定时循环触发某个方法
- 当Python和R遇上北京二手房(上)
- python初学 汉诺塔攻略实现
- 在ArcGIS中创建Python工具(二)
- Python核心编程-练习
- Python游戏服务器开发日记(六) 解决GIL难题!——Python再认识
- Python游戏服务器开发日记(五)skynet_messagequeue和skynet_timer