UnicodeDecodeError: 'ascii' codec can't decode byte 0xa3 in position 1: ordinal not in r

Posted 一天一点到

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UnicodeDecodeError: 'ascii' codec can't decode byte 0xa3 in position 1: ordinal not in r相关的知识,希望对你有一定的参考价值。

使用codecs模块

codecs模块能在处理字节流的时候提供很大帮助。你可以用定义的编码来打开文件并且你从文件里读取的内容会被自动转化为Unicode对象。

试试这个:

 

>>> importcodecs
>>> fh =codecs.open("/tmp/utf-8.txt","w","utf-8")
>>> fh.write(u"\u2013")
>>> fh.close()

 

 

import codecs
with codecs.open(file_path,"utf-8") as fp:
    for line in fp:

        log_obj = XAdnginxLog(p_line=line)

 

以上是关于UnicodeDecodeError: 'ascii' codec can't decode byte 0xa3 in position 1: ordinal not in r的主要内容,如果未能解决你的问题,请参考以下文章

python编码问题:UnicodeDecodeError: 'gbk' codec can't decode

python UnicodeDecodeError: 'ascii' codec can't decode byte ...

UnicodeDecodeError: 'ascii' codec can't decode byte错误解决方案

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcd

UnicodeDecodeError: 'utf-8' codec can't decode byte..问题

UnicodeDecodeError: 'utf-8' codec can't decode byte..问题