有關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,