|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
按照小甲鱼写的抄的,我的出现了Bug了,刚接触编程,有伙计可以帮一下忙么
应该是split()函数参数出问题了,代码如下():
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(girl)
boy_file.close()
girl_file.close()
def split_file(file_name):
f = open('E:\\test.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:
save_file(boy,girl,count)
boy = []
girl= []
count+=1
save_file(boy,girl,count)
f.close()
split_file('E:\\test.txt')
出现error如下:
Traceback (most recent call last):
File "D:/Technology/Py代码/fengehanshu.py", line 39, in <module>
split_file('E:\\test.txt')
File "D:/Technology/Py代码/fengehanshu.py", line 23, in split_file
(role,line_spoken) = each_line.split(':',1)
ValueError: not enough values to unpack (expected 2, got 1)
not enough values to unpack (expected 2, got 1),意思是说需要2个值,但是只有1个,each_line.split(':',1)应该以冒号为界把一句话分成两部分,如果那句话没有冒号就不行了,所以应该是test.txt的内容有问题,这个文件应该是除了等号行以外其他每行都含有至少1个冒号。
顺便说一下,函数split_file有参数,你却没用,而是直接打开文件,不大好,应该改成 f = open(file_name)才算正确。
|
|