Home  >  Article  >  Backend Development  >  python写入中英文字符串到文件的方法

python写入中英文字符串到文件的方法

WBOY
WBOYOriginal
2016-06-10 15:13:101424browse

本文实例讲述了python写入中英文字符串到文件的方法。分享给大家供大家参考。具体分析如下:

python中如果使用系统默认的open方法打开的文件只能写入ascii吗,如果要写入中文需要用到codecs模块,下面的代码向 c:/1.txt文件写入 ”你好,脚本之家 jb51.net“中文字符串

# -*- coding: utf-8 -*-
import codecs
content = u'你好,脚本之家 jb51.net'
f = codecs.open('c:/1.txt','w','utf-8')
f.write(content)

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn