【发布时间】:2018-04-28 20:29:12
【问题描述】:
我想使用 python-docx 获取一些 docx 文件的纯文本,但由于文本是用西班牙语编写的,所以我在重音方面遇到了困难。
我正在使用this answer阅读文字:
def getText(filename):
doc = docx.Document(filename)
fullText = []
for para in doc.paragraphs:
fullText.append(para.text('utf-8'))
return '\n'.join(fullText)
返回如下内容:
n\xc3\xbamero //should be número
有什么方法可以让我得到正确的重音文本?
当我尝试将此文本写入文件时:
file = open("/mnt/c/Users/lulas/Desktop/inSpanish/txt/course1.txt","w")
file.write(text)
我收到此错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xed' in position 27: ordinal not in range(128)
这是由于重音的读取/编码方式。
【问题讨论】:
标签: python character-encoding docx python-docx