求助大佬,求助
name_file = open('record.txt')list_boy = []
list_girl = []
count = 1
for i in name_file:
if i[:6] != '=========':
(role, line_spoken) = i.split(':', 1)
if role == '小甲鱼':
list_boy.append(line_spoken)
if role == '小客服':
list_girl.append(line_spoken)
else:
a = 'boy_' + str(count) + '.txt'
b = 'girl_' + str(count) + '.txt'
new_boy = open(a, 'w')
new_girl = open(b, 'w')
new_boy.writable(list_boy)
new_girl.writable(list_girl)
new_girl.close()
new_girl.close()
count += 1
list_boy = []
list_girl = []
name_file.close()
我(role, line_spoken) = i.split(':', 1)明明已经切片了为什么ValueError: not enough values to unpack (expected 2, got 1)总是报这个错误,脑壳痛。 本帖最后由 jackz007 于 2021-12-4 01:34 编辑
这个代码会告诉你具体是哪些行出的问题,帮助你找到错误原因和解决方法,如果你自己不会诊断,那就把屏幕显示信息贴出来看看。
name_file = open('record.txt')
list_boy = []
list_girl = []
count = 1
for i in name_file:
if i[:6] != '=========':
try:
(role, line_spoken) = i.split(':', 1)
if role == '小甲鱼':
list_boy.append(line_spoken)
if role == '小客服':
list_girl.append(line_spoken)
except Exception as e:
print(e)
print('error line = [' + i + ']')
else:
a = 'boy_' + str(count) + '.txt'
b = 'girl_' + str(count) + '.txt'
new_boy = open(a , 'w')
new_girl = open(b , 'w')
new_boy . writable(list_boy)
new_girl . writable(list_girl)
new_girl . close()
new_girl . close()
count += 1
list_boy = []
list_girl = []
name_file.close() jackz007 发表于 2021-12-4 01:32
这个代码会告诉你具体是哪些行出的问题,帮助你找到错误原因和解决方法,如果你自己不会诊断,那就 ...
not enough values to unpack (expected 2, got 1)
error line = [================================================================================
]
not enough values to unpack (expected 2, got 1)
error line = [================================================================================
]是这样的 K奔跑的蜗牛 发表于 2021-12-4 20:01
not enough values to unpack (expected 2, got 1)
error line = [=================================== ...
我知道了,是我if i[:6] != '========':这的等号超过了6个。 本帖最后由 jackz007 于 2021-12-4 20:10 编辑
if i[:6] != '=========':
数一下,引号里是几个 '=',如果不是 6 个就写错了,我数了一下,一共是 9 个,所以,这句代码应该这样改:
if i[:9] != '=========':
也就是把 6 改成 9 就可以了。
改完了再试。
页:
[1]