|
发表于 2019-1-11 14:41:59
|
显示全部楼层
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中是英文冒号
也没有空行
能帮忙检查一下吗 ,多谢 |
|