|
5鱼币
- f = open('/Users/xiaobingbing/Desktop/record.txt',encoding='utf-8')
- boy = []
- girl = []
- count = 1
- 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:
- #文件的分别保存操作
- file_name_boy = 'boy_' + str(count) + '.txt'
- file_name_girl = 'girl_' + str(count) + '.txt'
- boy_file = open(file_name_boy,'w',encoding='utf-8')
- girl_file = open(file_name_girl,'w',encoding='utf-8')
- boy_file.writelines(boy)
- girl_file.writelines(girl)
- boy_file.close()
- girl_file.close()
- boy = []
- girl = []
- count += 1
- 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()
- f.close()
复制代码
报错内容是:
- Traceback (most recent call last):
- File "/Users/xiaobingbing/Desktop/Python/第29讲-分割文本文件.py", line 40, in <module>
- boy_file.writelines(boy)
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
复制代码
请问各位大牛,我是得把系统默认代码改掉么?该怎么改?还是有其他的办法,涉及到文件编码问题我的mac总是出现这个问题
我用的是Mac os 10.12 Python版本是3.6.1 |
|