2014-02-13 37 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