UnicodeDecodeError: 'ascii' codec can't decode byte 0xa3 in position 1: ordinal not in range(128)
使用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)