python3中
此qr数据是utf-8编码的数据格式,并指定errors的错误处理方式errors='ignore'
在读,写数据的时候直接指定编码格式
with open(data_path,'r',encoding='utf-8',errors='ignore') as file_content:
整个读入和写进去不报错,且能看到错误处理方式为:
def split_qr(data_path,output_path):
with open(output_path,'a',encoding="utf-8") as f:
with open(data_path,'r',encoding='utf-8',errors='ignore') as file_content:
for line in file_content:
print("line in file_content: ") # 挨个输出文本每一行,比如 输出文本第一行:比较好哪方面的压力啊
print(line) # 之前的写print("line in file_content: ",line),输出会乱码可能是由于环境是python2的问题,
query_reply_arr = line.strip('\n').split('\t') # 原始数据的每一行,q r用之间用\t分割开了,每行的末尾其实有\n
if len(query_reply_arr) != 2: # 如果只有一个q,或只有一个r则跳过
continue
label=str('0')
out_line_q= '%s\t%s\n' % (label, query_reply_arr[0].strip(' '))#将label与content合并
f.write((out_line_q))
out_line_r= '%s\t%s\n' % (label, query_reply_arr[1].strip(' '))#将label与content合并
f.write((out_line_r))
split_qr("./part100_train_raw_data.txt","./splited_QR.txt")
卢老师的代码会有,很可能是由于其用的是python2.
llabel.encode("utf-8"), word_content.strip(' ').encode("utf-8")
待查python3编码和python2编码的区别
相关:
Python 解决 :NameError: name 'reload' is not defined 问题
https://blog.csdn.net/github_35160620/article/details/52206868
Python中读取txt文本出现“ 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence”的解决办法
https://blog.csdn.net/lqzdreamer/article/details/76549256