2014-02-13 21 views

回答

2

使用open功能codecs模块:

import codecs 

with codecs.open(logPath, encoding='utf8') as infile: 
    lines = infile.readlines() 

默认情况下,codecs.open功能,rb打开文件(阅读二进制)模式:

def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):

... 
    Files are always opened in binary mode, even if no binary mode 
    was specified. This is done to avoid data loss due to encodings 
    using 8-bit values. The default file mode is 'rb' meaning to 
    open the file in binary read mode. 
+0

我可以用这个INFILE =打开(logPath,encoding ='utf8') – speedyrazor

+0

这给了我错误:infile = open(logPath,encoding ='utf8') Ty peError:'encoding'是此函数的无效关键字参数 – speedyrazor

+0

现在使用infile = codecs.open(logPath,encoding ='utf8')任何人都可以看到这个问题吗? – speedyrazor