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