Python新手问题求助!!!!
代码:
boy = []
girl = []
count = 1
f = open('D:\\bug.txt')
for each_line in f:
if each_line[ :3] != '====':
(role,line_speak) = each_line.split(':',1)
if role == '小甲鱼':
boy.append(line_speak)
if role == '客服':
girl.append(line_speak)
else:
boy_file_name = 'boy_' + str(count) + '.txt'
girl_file_name = 'girl_' + str(count) + '.txt'
file_boy = open(boy_file_name,'w')
file_girl = open(girl_file_name,'w')
file_boy.writeline(boy)
file_girl.writeline(girl)
file_boy.close()
file_girl.close()
boy = []
girl = []
count += 1
f.close()
报错:
Traceback (most recent call last):
File "C:/Users/Admin/AppData/Local/Programs/Python/Python36/一个任务2.py", line 11, in <module>
(role,line_speak) = each_line.split(':',1)
ValueError: not enough values to unpack (expected 2, got 1)
求大神赐教!!{:5_105:}
出现这种错误提示,表示文本文件的此行无法分解为“:"前后的两部分。
有时是混用了中英文冒号,有时时把分隔行”=================“也去分解了。
你的错误是if each_line[ :3] != '====':,因为each_line[ :3]只取3位,所以不等式总是满足的,总去split 冬雪雪冬 发表于 2018-1-4 14:22
出现这种错误提示,表示文本文件的此行无法分解为“:"前后的两部分。
有时是混用了中英文冒号,有时时把分 ...
排除了中英冒号混用,也把切片增大了,还是不行
boy = []
girl = []
count = 1
f = open('D:\\bug.txt')
for each_line in f:
if each_line[ :8] != '====':
(role,line_speak) = each_line.split(';',1)
if role == '小甲鱼':
boy.append(line_speak)
if role == '客服':
girl.append(line_speak)
else:
boy_file_name = 'boy_' + str(count) + '.txt'
girl_file_name = 'girl_' + str(count) + '.txt'
file_boy = open(boy_file_name,'w')
file_girl = open(girl_file_name,'w')
file_boy.writeline(boy)
file_girl.writeline(girl)
file_boy.close()
file_girl.close()
boy = []
girl = []
count += 1
f.close()
if each_line[ :8] != '====':
要和8个”=“对应 可能问题:
1 文件中有空行
2 冒号不匹配,比如文件中是中文冒号,而切片的时候是英文冒号
3if判断不匹配,切片是几个右边就需要写几个等号:
if each_line[:6] != '=======': 冬雪雪冬 发表于 2018-1-4 14:41
if each_line[ :8] != '====':
要和8个”=“对应
无比感激{:5_91:}{:5_91:}{:5_106:}{:5_106:}{:5_106:}
页:
[1]