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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-10 15:13:10
Original
1499 people have browsed it

本文实例讲述了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)

Copy after login

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

Related labels:
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template