|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是文档:
小客服:小甲鱼,今天有客户问你有没有女朋友?
小甲鱼:咦??
小客服:我跟她说你有女朋友了!
小甲鱼:。。。。。。
小客服:她让你分手后考虑下她!然后我说:"您要买个优盘,我就帮您留意下~”
小甲鱼:然后呢?
小客服:她买了两个,说发一个货就好~
小甲鱼:哦。。。。。。你真牛!
小客服:那是,谁让我是鱼C最可爱小客服嘛~
小甲鱼:下次有人想调戏你我不阻止~
小客服:滚!!!
=====================================================
小客服:小甲鱼,有个评论很好笑哈。
小甲鱼:哦?
小客服:“有了小甲鱼,以后妈妈再也不用担心我的学习了~”
小甲鱼:哈哈哈,我看到丫,我还发微博了呢~
小客服:嗯嗯,我看了你的微博丫~
小甲鱼:呦西~
小客服:那个有条回复“左手拿着小甲鱼,右手拿着打火机,哪里不会点哪里,so easy^_^”
小甲鱼:T_T
=====================================================
小客服:小甲鱼,今天一个会员想找你
小甲鱼:哦?什么事?
小客服:他说你一个学生月薪已经超过12K了!!
小甲鱼:哪里的?
小客服:上海的
小甲鱼:那正常,哪家公司?
小客服:他没说呀。
小甲鱼:哦
小客服:老大,为什么我工资那么低啊??是时候涨涨工资了!!
小甲鱼:啊,你说什么?我在外边呢,这里好吵吖。。。。。。
小客服:滚!!!
这是test1
f = open('record.txt','r',encoding='utf-8')
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()
boy = []
girl = []
count += 1
f.close()
这是test2
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):
boy = []
girl = []
count = 1
f = open(file_name)
for each_line in f:
if each_line[:6] != '=======':
(role,line_spoken) = each_line.spoken(':',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('record.txt')
提问:原文档和代码中我都按照英文‘:’来的然后test1运行后idle报错:
Traceback (most recent call last):
File "D:\python编写\test_1.py", line 9, in <module>
(role, line_spoken) = each_line.split(':', 1)
ValueError: not enough values to unpack (expected 2, got 1)
但是可以得到正常的_1,_2文件没有_3文件
运行test2 不报错但是只能得到_1文件,文件还是个空的
通宵在线等 谢谢带个带姐萌
本帖最后由 jackz007 于 2020-3-1 02:04 编辑
问题应该出在文件 "record.txt" 最后可能有空行存在,这个代码要求这个文件中不可以有任何一个空行。所有的内容只能是两种形式之一,一种是文本中至少有一个 ':' 字符,另一种就是文本内容由至少 6 个连续的 '=' 字符开头,否则,就会发生楼主所说的那种错误。
可以用我整理的代码试:
- #-*-coding:gbk-*-
- 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 = [] , [] , 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()
复制代码
如果出现文件编码错误,就把这一句
改成这样
- f = open('record.txt' , encoding = 'UTF-8')
复制代码
|
|