python编码问题记录2

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  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值