|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么我在分割语句那里总是错误,提示 TypeError: a bytes-like object is required, not 'str'
- import pickle
- def split_file(file_name):
- boy = []
- girl = []
- count = 1
- f = open('record.txt', 'rb')
- for each_line in f:
- if each_line[:6] != '======':
- (name, words) = each_line.split(':', 1) #就是这个':'
- if name == '小甲鱼':
- boy.append(words)
- if name == '小客服':
- girl.append(words)
- else:
- boy_file_name = 'boy_' + str(count) + '.txt'
- girl_file_name = 'girl_' + str(count) + '.txt'
- boy_file = open(boy_file_name, 'wb')
- girl_file = open(girl_file_name, 'wb')
- pickle.dump(boy, boy_file)
- pickle.dump(girl, girl_file)
- boy_file.close()
- girl_file.close()
- count += 1
- boy = []
- girl = []
- boy_file_name = 'boy_' + str(count) + '.txt'
- girl_file_name = 'girl_' + str(count) + '.txt'
- boy_file = open(boy_file_name, 'wb')
- girl_file = open(girl_file_name, 'wb')
- pickle.dump(boy, boy_file)
- pickle.dump(girl, girl_file)
- boy_file.close()
- girl_file.close()
- split_file('record.txt')
复制代码
第9行: - f = open('record.txt', 'rb')
复制代码你打开方式是 rb,读出来的是 bytes。
|
|