python简单文件操作

有关python的文件读写操作。

将中文写入文件,并且将文件中的中文读出来:

#coding:utf-8

CODEC = 'utf-8'
FILE = 'unicode.txt'

hello_out = u"世界,你好\n"
bytes_out = hello_out.encode(CODEC)
f = open(FILE, "w")
f.write(bytes_out)
f.close()

f = open(FILE, "r")
bytes_in = f.read()
f.close()
hello_in = bytes_in.decode(CODEC)
print hello_in,




发布了32 篇原创文章 · 获赞 16 · 访问量 15万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章