我的代码如下所示:
for file in glob.iglob(os.path.join(dir, '*.txt')):
print(file)
with codecs.open(file,encoding='latin-1') as f:
infile = f.read()
with codecs.open('test.txt',mode='w',encoding='utf-8') as f:
f.write(infile)
我处理的文件是用拉丁语-1编码的(显然我不能用UTF-8打开它们)。但是我想用utf-8来写结果文件。
但是这个:
<Trans audio_filename="VALE_M11_070.MP3" xml:lang="español">
<Datos clave_texto=" VALE_M11_070" tipo_texto="entrevista_semidirigida">
<Corpus corpus="PRESEEA" subcorpus="ESESUMA" ciudad="Valencia" pais="España"/>
取而代之的是(在gedit中):
<Trans audio_filename="VALE_M11_070.MP3" xml:lang="espa뇃漀氀∀㸀ഀ㰀䐀愀琀`漀猀 挀氀愀瘀攀开琀攀砀琀漀㴀∀ 嘀䄀䰀䔀开䴀开 㜀
如果我把它打印在终端上,它就会正常显示。
更令人困惑的是,当我用LibreOffice Writer打开生成的文件时,会得到什么:
<#T#r#a#n#s# (and so on)
那么,如何正确地将拉丁-1字符串转换为utf-8字符串呢?在python2中,这很简单,但是在python3中,我觉得很困惑。
我已经尝试过不同的组合:
#infile = bytes(infile,'utf-8').decode('utf-8')
#infile = infile.encode('utf-8').decode('utf-8')
#infile = bytes(infile,'utf-8').decode('utf-8')
但不知怎么的,我总是得到同样奇怪的结果。
提前谢谢!
编辑:这个问题不同于评论中链接的问题,因为它涉及的是Python3,而不是Python2.7。
我在这件事上找到了一半。这不是你想要/需要的,但可能会帮助其他人朝着正确的方向。。。
*注detection
对于Python3.6:
相关问题 更多 >
编程相关推荐