|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在零基础学python第29讲中,需要下载record.txt文件(txt文件内容见图片)。
问题是!!!!!
我用的是sublime text2编辑器,record.txt能在该编辑器中打开,出现的不是中文,也就是乱码了....(见图片)
乱码后,我完全没有办法对这个record.txt读取,导致我的代码运行结果为空(见图片),代码如下(我觉得我的代码应该是没有问题的)。
- import sys
- reload(sys)
- sys.setdefaultencoding("utf-8")
- def save_file(boy,girl,count):
- file_name_boy='boy_'+str(count)+'.txt'
- file_name_girl='girl_'+str(count)+'.txt'
- boy_file=open(file_name_boy,'w')
- boy_file.writelines(boy)
- boy_file.close()
- girl_file=open(file_name_girl,'w')
- girl_file.writelines(girl)
- girl_file.close()
- def split_file(file_name):
- f=open(file_name)
- boy=[]
- girl=[]
- count=1
- for eachline in f:
- if eachline[:3]!='===':
- (role,content)=eachline.split(':',1)
- if role=='小甲鱼':
- boy.append(content)
- if role=='小客服':
- girl.append(content)
- else:
- save_file(boy,girl,count)
- boy=[]
- girl=[]
- count+=1
- save_file(boy,girl,count)
- f.close()
- split_file('/Users/zhangying/Desktop/221-code/Alearning/record.txt')
复制代码
是不是所有非自己生成的含有中文字符的txt文件在sublime text编辑器中打开都会乱码?如何解决呢?
题外话:平常我自己生成的含中文字符的txt文件无论是在sublime text 还是用系统自带的文本编辑器都能够正确的不乱码的打开。
|
|