029文档练习分割那里说错误代码:无法解码 求助
本帖最后由 千寻大人 于 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
有大神帮忙解决下吗 我搞了好久没搞明白 不知道是不是系统问题
编码问题,在open 里面加上参数 encoding = 'utf-8' 试试看
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',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') Twilight6 发表于 2020-7-12 10:21
编码问题,在open 里面加上参数 encoding = 'utf-8' 试试看
我试了 不行 这次说AttributeError:“list”对象没有属性“close” Twilight6 发表于 2020-7-12 10:21
编码问题,在open 里面加上参数 encoding = 'utf-8' 试试看
之前说 for fenge in f:
“非法的GBDeeCoDee序列中的”GBDeec“错误代码:无法解码” 的问题 千寻大人 发表于 2020-7-12 10:23
我试了 不行 这次说AttributeError:“list”对象没有属性“close”
把第 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')
Twilight6 发表于 2020-7-12 10:24
把第 12 行的 girl.close() 改成 girl2.close()
可以了 谢谢大神 我在研究研究 千寻大人 发表于 2020-7-12 10:27
可以了 谢谢大神 我在研究研究
客气了
页:
[1]