|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 ynoneone 于 2022-3-22 14:17 编辑
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)
boy_file,writelines(girl)
boy_file.close()
girl_file.close()
boy = []
girl = []
count +=1
f.close()
哪里错了。求指教
Traceback (most recent call last):
File "H:/python练习/006.py", line 11, in <module>
(role,line_spoken) = 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[:3] != '===':
- (role, line_spoken) = each_line.split(':', 1)
- if role == '甲':
- boy.append(line_spoken)
- if role == '乙':
- girl.append(line_spoken)
- else:
- file_name_boy = '甲_' + str(count) + '.txt'
- file_name_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) # 不是 还有这是 girl_file不是boy_file
- boy_file.close()
- girl_file.close()
- boy = []
- girl = []
- count += 1
- # 因为最后没有 【=====】 所以还要再手动写入
- file_name_boy = '甲_' + str(count) + '.txt'
- file_name_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) # 不是 还有这是 girl_file不是boy_file
- boy_file.close()
- girl_file.close()
- f.close()
复制代码
|
|