|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我的代码:
- import pickle
- def dump(count,boy,girl):
- open('boy_'+count+'.txt')
- open('girl_'+count+'.txt')
- pickle.dump(boy,'boy_'+count+'.txt')
- pickle.dump(girl,'girl_'+count+'.txt')
- 'boy_'+count+'.txt'.close()
- 'girl_'+count+'.txt'.close()
- def split(file):
- count = 1
- boy = []
- girl = []
- for each in file:
- if '====' not in each:
- (name,spoken) = each.split(':',1)
- if name == '小甲鱼':
- boy.append(spoken)
- elif name == '小客服':
- girl.append(spoken)
- else:
- dump(count,boy,girl)
- boy = []
- girl = []
- dump(count,boy,girl)
- file.close()
- file = open('record.txt','r',encoding='utf-8')
- split(file)
复制代码
报错代码:
- Traceback (most recent call last):
- File "D:/编程练习/课后作业(永久储存,第31讲,动动手第0题).py", line 32, in <module>
- split(file)
- File "D:/编程练习/课后作业(永久储存,第31讲,动动手第0题).py", line 16, in split
- for each in file:
- File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\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
复制代码
- import pickle
-
- 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, 'wb') # 记得一定要加 b 吖
- girl_file = open(file_name_girl, 'wb') # 记得一定要加 b 吖
-
- pickle.dump(boy, boy_file)
- pickle.dump(girl, girl_file)
-
- boy_file.close()
- girl_file.close()
-
- def split_file(file_name):
- count = 1
- boy = []
- girl = []
-
- f = open(file_name)
-
- 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:
- save_file(boy, girl, count)
-
- boy = []
- girl = []
- count += 1
-
- save_file(boy, girl, count)
- f.close()
-
- split_file('record.txt')
复制代码
|
|