|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
你好!这个问题你是怎么解决的?
为啥我的不行啊!
count=1
boy=[]
girl=[]
f=open(r'C:\Users\****\Desktop\record.txt','r',encoding='utf=8')
for each_lines in f:
if each_lines[:6] != '======':
(role,line_spoken)=each_lines.split(':',1)
if role=='小客服:':
girl.append(line_spoken)
if role=='小甲鱼:':
boy.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', encoding='utf=8')
girl_file = open(file_name_girl, 'w', encoding='utf=8')
boy_file.writelines(girl)
girl_file.writelines(boy)
boy=[]
girl=[]
count+=1
file_name_boy='boy'+str(count)+'.txt'
file_name_girl = 'girl' + str(count) + '.txt'
boy_file = open((r'C:\Users\****\Desktop\%s'%file_name_boy), 'w', encoding='utf=8')
girl_file = open((r'C:\Users\****\Desktop\%s'%file_name_girl), 'w', encoding='utf=8')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
f.close()
提示的错误类型是:
Traceback (most recent call last):
File "C:/PYTHON/20220107/python07.05.py", line 8, in <module>
(role,line_spoken)=each_lines.split(':',1)
ValueError: not enough values to unpack (expected 2, got 1)
Process finished with exit code 1
也就是这行代码有问题:
for each_lines in f:
if each_lines[:6] != '======':
(role,line_spoken)=each_lines.split(':',1)
if role=='小客服:':
原文件:
小客服:小甲鱼,有个好评很好笑哈。
========
小甲鱼:哦?
========
小客服:“有了小甲鱼,以后妈妈再也不用担心我的学习了~”
========
小甲鱼:看到了丫,我还发微博了呢~
========
小客服:嗯嗯,我看到了你的微博~
========
小甲鱼:哟西~
========
小客服:哪里不会点哪里··~~
========
运行这个代码就可以帮助你直接找出原因 count=1
boy=[]
girl=[]
f=open(r'C:\Users\****\Desktop\record.txt','r',encoding='utf=8')
for each_lines in f:
if each_lines[:6] != '======':
try:
(role,line_spoken)=each_lines.split(':',1)
if role=='小客服:':
girl.append(line_spoken)
if role=='小甲鱼:':
boy.append(line_spoken)
except Exception as e:
print(each_lines)
print(e)
else:
file_name_boy='boy'+str(count)+'.txt'
file_name_girl = 'girl' + str(count) + '.txt'
boy_file = open(file_name_boy, 'w', encoding='utf=8')
girl_file = open(file_name_girl, 'w', encoding='utf=8')
boy_file.writelines(girl)
girl_file.writelines(boy)
boy=[]
girl=[]
count+=1
file_name_boy='boy'+str(count)+'.txt'
file_name_girl = 'girl' + str(count) + '.txt'
boy_file = open((r'C:\Users\****\Desktop\%s'%file_name_boy), 'w', encoding='utf=8')
girl_file = open((r'C:\Users\****\Desktop\%s'%file_name_girl), 'w', encoding='utf=8')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
f.close()
|
|