新人求助,《零基础学习python》中8.1.6的问题
你好!这个问题你是怎么解决的?为啥我的不行啊!
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() 因为文件中的是中文的冒号 【:】
你的代码是英文的冒号 【:】
改一下就好了 谢谢回复!按你的建议我把所有的冒号都改成了统一的格式,也按你修改后的程序代码,编译了一下:
现在是程序不报错,但是分别生成boy1.txt,boy2.txt,boy3.txt,boy4.txt ,boy5.txt,boy6.txt,boy7.txt,boy8.txt和girl1.txt,girl2.txt,girl3.txt,girl4.txt,girl5.txt,girl6.txt,girl7.txt,girl8.txt 文件,
但问题是这些文件都是空文件 你这代码我看了有点乱呀,for循环后面的是在一起吗,看起来缩进不一样耶
我就说一下for循环里面的,你的else中打开两个文件。一个是boy一个girl,
按照你提供的文本看来,程序先读取保留小甲鱼说的话。读取下一行执行了else,那么只有girl文件有内容,boyl没有,后面的也是如此
按理说应该是一个有一个没有的{:10_245:}
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 谢谢各位学友!
通过反复阅读零基础学python,发现了代码的问题:
造成空文件夹的原因是split(':',1),以“:”分割,那么分割后就不会有冒号了。小客服和小甲鱼后面不能有冒号!
页:
[1]