1
我正在使用下面的代碼,但需要打開它才能使用指定的utf-8進行讀取。我該怎麼做?使用UTF-8打開文件進行讀取
infile = file(logPath)
lines = infile.readlines()
我正在使用下面的代碼,但需要打開它才能使用指定的utf-8進行讀取。我該怎麼做?使用UTF-8打開文件進行讀取
infile = file(logPath)
lines = infile.readlines()
使用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.
我可以用這個INFILE =打開(logPath,encoding ='utf8') – speedyrazor
這給了我錯誤:infile = open(logPath,encoding ='utf8') Ty peError:'encoding'是此函數的無效關鍵字參數 – speedyrazor
現在使用infile = codecs.open(logPath,encoding ='utf8')任何人都可以看到這個問題嗎? – speedyrazor