with open('1.txt')as f:
for i in f.readlines():
needed=i.split(':',1)
with open('need.txt','a')as f1:
f1.write('{}:{}'.format(needed[0],needed[1]))
这样会报错,'gbk' codec can't decode byte
只能加上
with open('1.txt',encoding='utf-8')as f:
for i in f.readlines():
needed=i.split(':',1)
with open('need.txt','a')as f1:
f1.write('{}:{}'.format(needed[0],needed[1]))
指定编码方式才可以,然后输出的need.txt里面有乱码,要转化成GBK编码才能正常显示。1.txt编码方式是utf-8。
系统的默认编码是utf-8
有什么办法让open函数默认就是utf8编码呢,而不需要手动指定。