|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
将record.txt文件中 小甲鱼和小客服 的对话分别保存 boy.txt girl.txt
代码:
open_file = open('F:\\record.txt')
count = 1
list_boy = []
list_girl = []
for each_line in open_file:
(name,content) = each_line.split(':')
if name == '小甲鱼':
list_boy.append(content + '\n')
elif name == '小客服':
list_girl.append(content + '\n')
elif each_file[3] == '===' :
write_boy = open('boy' + str(count) + '.txt','w')
write_girl = open('girl' + str(count) + '.txt','w')
write_boy.writelines(list_boy)
write_girl.writelines(list_girl)
count += 1
list_boy = []
list_girl = []
程代码报错:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\pickle用法.py", line 9, in <module>
(name,content) = each_line.split(':')
ValueError: too many values to unpack (expected 2)
这是为什么
本帖最后由 sky 于 2018-1-29 11:35 编辑
split返回的列表元素超过两个了 说明有一行有两个冒号了
split可以指定分割几次 所以分割一次就可以了- >>> a = "a:b:c"
- >>> a.split(":",1)
- ['a', 'b:c']
- >>>
复制代码
|
|