第八单元练习时遇到的UnicodeDecodeError错误
代码如下:#text_exercise_1.py
count=1
boy=[]
girl=[]
f = open("e:\\教育\\文档操作练习1.txt")
for each_line in f:
if each_line[:6]!="================":
(role,line_spoken) = each_line.split(":",1)
if role == "小甲鱼":
boy.append(line_spoken)
if role == "客服":
girl.append(line_spoken)
else:
file_name_boy = "boy_."+ str(count)+".txt"
file_name_girl = "girl_."+ str(count)+".txt"
boy_file = open(file_name_boy,"w")
girl_file = open(file_name_girl,"w")
boy_file.writelines(boy)
girl_file.writelines(girl)
boy = []
girl = []
count += 1
file_name_boy = "boy"+str(count)+".txt"
file_name_girl = "girl_."+ str(count)+".txt"
boy_file = open(file_name_boy,"w")
girl_file = open(file_name_girl,"w")
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
f.close()
运行后出现异常:UnicodeDecodeError:“gbk” codec can"t decode byte 0xbf in position 2: illegal multibyte sequence.
求教不知道是文档问题还是代码问题。谢谢! f = open("e:\\教育\\文档操作练习1.txt",encoding = 'utf-8') 谢谢,我试试 不过又抛出个错误:Traceback (most recent call last):
File "E:\教育\python学习\py_8_1.py", line 8, in <module>
(role,line_spoken) = each_line.split(":",1)
ValueError: not enough values to unpack (expected 2, got 1)
修改了文本文件中每行前边的空格还有其中的中文格式下的“:”,还是没有解决。头真大{:5_104:} 修改了文档部分地方,终于解决了。谢谢
页:
[1]