|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 miccky 于 2017-3-11 15:14 编辑
我按照 小甲鱼 的代码 写了一遍为什么会出现以下错误?
操作系统 mac
============ RESTART: /Users/shanfengsun/Documents/Python/cai.py ============
Traceback (most recent call last):
File "/Users/shanfengsun/Documents/Python/cai.py", line 7, in <module>
for each_line in f:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 21: ordinal not in range(128)
讲是什么ASCII码错误
代码如下:
f = open("test.txt")
boy = []
girl = []
count = 1
for each_line in f:
if each_line[:6] != "======":
(role,line_spoken) = each_line.split(":",1)
if role == "A":
boy.append(line_spoken)
if role == "B":
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
boy_file.close()
girl_file.close()
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
boy_file.close()
girl_file.close()
f.close()
哪位大神能给我解答一下,感谢了
|
|