文件内容归类出错 ValueError: not enough values to unpack (expected 2, got 1)
代码:def save_file(boy,girl,count):
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(gril)
boy_file.close()
girl_file.close()
def split_file(file_name):
f=open('F:\\record.txt')
boy=[]
girl=[]
count=1
for each_line in f:
if each_line[:6] !='======':
(role,line_spoken)=each_line.split(':',1)
if role == '小甲鱼':
boy.append(line_spoken)
if role == '小客服':
boy.append(line_spoken)
else:
save_file(boy,girl,count)
boy=[]
girl=[]
count+=1
save_file(boy,girl,count)
f.close()
split_file('record.txt')
报错:
Traceback (most recent call last):
File "F:\Python笔记\test-01.py", line 35, in <module>
split_file('record.txt')
File "F:\Python笔记\test-01.py", line 22, in split_file
(role,line_spoken)=each_line.split(':',1)
ValueError: not enough values to unpack (expected 2, got 1)
再三仔细和小甲鱼的教学视频比对,没有错啊,为什么我的运行就会报错呢。。。
麻烦各位前辈指出错误,非常感谢。
split是以':'为标志分为两个字符串,如果找不到':'就会报错。
唯一的原因是文件中用的是中文':' 冬雪雪冬 发表于 2016-11-15 16:10
split是以':'为标志分为两个字符串,如果找不到':'就会报错。
唯一的原因是文件中用的是中文':'
多谢前辈提醒,果然文件中“:”是中文的,还是自己不细心,非常感谢!{:10_277:} 冬雪雪冬 发表于 2016-11-15 16:10
split是以':'为标志分为两个字符串,如果找不到':'就会报错。
唯一的原因是文件中用的是中文':'
Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: No you haven't!
Other Man: Yes I have.
Man: When?
Other Man: Just now.
============
Man: No you didn't!
Other Man: Yes I did!
Man: You didn't!
Other Man: I'm telling you, I did!
Man: You did not! 这是我的文件
import os
os.chdir('F:/')
f = open('Test.txt')
boy = []
girl = []
count = 1
def save_file(boy,girl,count):
file_name_boy = 'boy_' + str(count) + '.txt'
file_name_girl = 'girl_' + str(count) + '.txr'
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()
def split_file(filename):
boy = []
girl = []
count = 1
for each_line in f:
if each_line[:6] != '=======':
(role,line_spoken) = each_line.split(':',1)
if role == 'Man':
boy.append(line_spoken)
elif role == 'Other Man':
girl.append(line_spoken)
else:
save_file(boy,girl,count)
boy = []
girl = []
count += 1
save_file(boy,girl,count)
f.close()
split_file('Test.txt') 所使用的代码和上面那位同学的差不多但也遇到了同样的问题,我看了源文件里的: 是英文的但还是有错,请问错在哪里呢 夜深听雨 发表于 2017-7-6 10:29
Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: No you haven't! ...
你的错误在这行:
if each_line[:6] != '=======':
用[:6]去匹配7个等号,总是True,所以对等号行也去split,当然找不到冒号了。 冬雪雪冬 发表于 2017-7-6 15:42
你的错误在这行:
if each_line[:6] != '=======':
用[:6]去匹配7个等号,总是True,所以对等号行也去s ...
可是我删了一个':'之后还是显示同样错误,是不是我文本的格式不对啊 夜深听雨 发表于 2017-7-6 17:36
可是我删了一个':'之后还是显示同样错误,是不是我文本的格式不对啊
我试了你的程序只要把if语句改为6个等号就可以了 冬雪雪冬 发表于 2017-7-6 22:16
我试了你的程序只要把if语句改为6个等号就可以了
懂了,谢谢啦 夜深听雨 发表于 2017-7-7 08:47
懂了,谢谢啦
我也遇到了同样的问题,现在已经确定record.txt中的:是英文的,但是还是报错:
Traceback (most recent call last):
File "D:\study\python\exercise\test_2.py", line 9, in <module>
(role,words) = each_line.split(':',1)
ValueError: not enough values to unpack (expected 2, got 1)
您是怎么解决的呢,求帮助,谢谢!
f = open('record.txt')
boy = []
girl = []
count = 1
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_file.close()
girl_file.close() #初始化
boy = []
girl = []
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()
#文件的分别保存操作
还是报错:
== RESTART: E:\OneDrive - University of Oklahoma\Python\视频练习\029\test_1.py ==
Traceback (most recent call last):
File "E:\OneDrive - University of Oklahoma\Python\视频练习\029\test_1.py", line 9, in <module>
(role, line_spoken)=each_line.split(':', 1)
ValueError: not enough values to unpack (expected 2, got 1)
>>>
我检查了文件中的冒号是英文的
请指教多谢
我也存在相同的问题,或许你可以检查一下.txt文件和代码中的等号是否匹配 冬雪雪冬 发表于 2017-7-6 22:16
我试了你的程序只要把if语句改为6个等号就可以了
f = open('/Users/shuoguo/Desktop/xrz/text3.txt')
xrz = []
xry = []
cound = 1
for each_line in f:
if each_line[:6] != '======':
(role, line_spoken) = each_line.split(':', 1)
if role == '徐睿泽':
xrz.append(line_spoken)
if role == '徐睿阳':
xry.append(line_spoken)
else:
file_name_xrz = 'xrz_' + str(cound) + '.txt'
file_name_xry = 'xry_' + str(cound) + '.txt'
xrz_file = open('/Users/shuoguo/Desktop/xrz/' + file_name_xrz, 'w')
xry_file = open('/Users/shuoguo/Desktop/xrz/' + file_name_xry, 'w')
xrz_file.writelines(xrz)
xry_file.writelines(xry)
xrz = []
xry = []
cound += 1
f.close()
页:
[1]