求助!!零基础python,一个任务那儿这样子报错是什么原因啊
Traceback (most recent call last):File "D:/自学python/文件/课堂练习/shishi.py", line 7, in <module>
for each_line in f:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 4: illegal multibyte sequence
就是照着视频里敲的代码,但是就是跑不动{:10_250:}{:10_250:}
源码发一下 本帖最后由 Zeus213 于 2020-9-27 20:21 编辑
1 编码问题,不用utf-8么 巴巴鲁 发表于 2020-9-27 20:11
源码发一下
count = 1
boy = []
girl = []
f = open(r"D:\自学python\文件\课堂练习\test.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 = 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()
疾风怪盗 发表于 2020-9-27 20:21
编码问题,不用utf-8么
代码在下面
你看吧,我先溜了{:10_245:} 疾风怪盗 发表于 2020-9-27 20:21
编码问题,不用utf-8么
是不是这样:f = open(r"D:\自学python\文件\课堂练习\test.txt",encode = “UTF-8”)
用了之后,又出问题了
Traceback (most recent call last):
File "D:/自学python/文件/课堂练习/shishi.py", line 9, in <module>
(role, line_spoken) = each_line.split(":",1)
ValueError: not enough values to unpack (expected 2, got 1) 巴巴鲁 发表于 2020-9-27 20:28
代码在下面
你看吧,我先溜了
大佬救救新人小白吧,我看了好久不知道该怎么搞 Zeus213 发表于 2020-9-27 20:31
大佬救救新人小白吧,我看了好久不知道该怎么搞
我也没学几天,还是小萌新{:10_277:} Zeus213 发表于 2020-9-27 20:29
是不是这样:f = open(r"D:\自学python\文件\课堂练习\test.txt",encode = “UTF-8”)
用了之后,又出 ...
你这个报错,就不是编码了,很多原因,打印一下循环的元素看看吧,应该是split出错了吧 啊啊啊,问题解决了,原来是txt里的冒号和代码里的冒号不一样,一个是中文冒号,一个是英文里的冒号
页:
[1]