|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 chenlong029 于 2020-3-29 22:53 编辑
各位大神,小白请教一下。
我照着小甲鱼的视频里的教程写的,但是报错了。代码如下:
def split_file(file_name):
f = open('record.txt')
boy = []
girl = []
count = 1
for each_line in f:
if each_line[:6] != '======':
(role, line_spoke) = each_line.split(':', 1)
if role == '小甲鱼':
boy.append(line_spoke)
if role == '小客服':
girl.append(line_spoke)
else:
save_file(boy, girl, count)
boy = []
gril = []
count += 1
save_file(boy, girl, count)
f.close()
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()
split_file('record.txt')
报错如下:
Traceback (most recent call last):
File "/Users/Desktop/tboy_girl.py", line 36, in <module>
split_file('record.txt')
File "/Users/Desktop/tboy_girl.py", line 7, in split_file
for each_line in f:
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 2: invalid start byte
然后我在open file这里加上了 encoding='gbk',不会报错。
def split_file(file_name):
f = open('record.txt', encoding='gbk')
boy = []
girl = []
count = 1
for each_line in f:
if each_line[:6] != '======':
(role, line_spoke) = each_line.split(':', 1)
if role == '小甲鱼':
boy.append(line_spoke)
if role == '小客服':
girl.append(line_spoke)
else:
save_file(boy, girl, count)
boy = []
gril = []
count += 1
save_file(boy, girl, count)
f.close()
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()
split_file('record.txt')
但是生成的6个文件中,boy的三个文件都是正常的,girl的3个文件,不对。girl_1正常,但是,girl_2包含girl_1的内容,girl_3包含gir_2 和 girl_1的内容。
girl_1.txt:
girl_2.txt:
girl_3.txt:
请大神们帮忙看一下这是怎么回事,哪里出了问题,谢谢了!
|
|