|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 千寻大人 于 2020-7-12 10:21 编辑
def save_file(boy,girl,count):
boy1 = 'boy_'+ str(count) + '.txt'
girl1 = 'girl_'+ str(count) + '.txt'
boy2 = open(boy1,'w')
girl2 = open(girl1,'w')
boy2.writelines(boy)
girl2.writelines(girl)
boy2.close()
girl.close()
def split_file(file_name):
f = open(r'G:\python\record.txt')
boy = []
girl = []
count = 1
for fenge in f:
if fenge[:6] != '======':
(shanchu,fengede) = fenge.split(':',1)
if shanchu == '小甲鱼':
boy.append(fengede)
if shanchu == '小客服':
girl.append(fengede)
else:
save_file(boy,girl,count)
# 重新初始化
boy = []
girl = []
count += 1
save_file(boy,girl,count)
f.close()
split_file('record.txt')
说我对于fenge in f:
“非法的GBDeeCoDee序列中的”GBDeec“错误代码:无法解码”
Traceback (most recent call last):
File "G:\python\文档练习.py", line 38, in <module>
split_file('record.txt')
File "G:\python\文档练习.py", line 20, in split_file
for fenge in f:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 4: illegal multibyte sequence
有大神帮忙解决下吗 我搞了好久没搞明白 不知道是不是系统问题
把第 12 行的 girl.close() 改成 girl2.close()
def save_file(boy, girl, count):
boy1 = 'boy_' + str(count) + '.txt'
girl1 = 'girl_' + str(count) + '.txt'
boy2 = open(boy1, 'w')
girl2 = open(girl1, 'w')
boy2.writelines(boy)
girl2.writelines(girl)
boy2.close()
girl2.close()
def split_file(file_name):
f = open(r'G:\python\record.txt', encoding='utf-8')
boy = []
girl = []
count = 1
for fenge in f:
if fenge[:6] != '======':
(shanchu, fengede) = fenge.split(':', 1)
if shanchu == '小甲鱼':
boy.append(fengede)
if shanchu == '小客服':
girl.append(fengede)
else:
save_file(boy, girl, count)
# 重新初始化
boy = []
girl = []
count += 1
save_file(boy, girl, count)
f.close()
split_file('record.txt')
|
|