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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章