测试成功的Python中文文件读写脚本
1#coding=utf-8
2import os
3import codecs
4
5def writefile(fn, v_ls):
6 f = codecs.open(fn, 'wb', 'utf-8')
7 for i in v_ls:
8 f.write(i + os.linesep)
9 f.close()
10
11def readfile(fn):
12 f = codecs.open(fn,'r','utf-8')
13 ls = [ line.strip() for line in f]
14 for i in ls :
15 print i.encode('gbk')
16 f.close()
17fn='./intimate.txt'
18v_ls=[u"中文"]
19writefile(fn,v_ls)
20readfile(fn)
21
22#代码本身也要存成utf-8格式。
23
24
2import os
3import codecs
4
5def writefile(fn, v_ls):
6 f = codecs.open(fn, 'wb', 'utf-8')
7 for i in v_ls:
8 f.write(i + os.linesep)
9 f.close()
10
11def readfile(fn):
12 f = codecs.open(fn,'r','utf-8')
13 ls = [ line.strip() for line in f]
14 for i in ls :
15 print i.encode('gbk')
16 f.close()
17fn='./intimate.txt'
18v_ls=[u"中文"]
19writefile(fn,v_ls)
20readfile(fn)
21
22#代码本身也要存成utf-8格式。
23
24