"UnicodeEncodeError: 'charmap' codec can't encode characters" when trying to parse .xlsx by openpyxl

"UnicodeEncodeError: 'charmap' codec can't encode characters" when trying to parse .xlsx by openpyxl

--- 更新 ---

我认为这个控制台日志解决了这个问题,但是仍然不清楚如何解决它:

>>> workbook = openpyxl.load_workbook('data.xlsx')
>>> worksheet = workbook.active
>>> worksheet['A2'].value
u'\u041c\u0435\u0448\u043e\u043a \u0434\u0435\u043d\u0435\u0433'
>>> print worksheet['A2'].value
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-4: character maps to <undefined>

--- 结束更新 ---

我正在尝试使用 openpyxl 打印一些 .xlsx 单元格的值:

import openpyxl
workbook = openpyxl.load_workbook(filename='puzzles.xlsx')
worksheet = workbook.active
for row in worksheet.iter_rows('A2:K5'):
    print row[0].value

这会导致以下错误:

Traceback (most recent call last):
  File "xls_import.py", line 8, in <module>
    print row[0].value
  File "C:\Python27\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-4: character maps to <undefined>

据我所知,XLSX 编码为 UTF-8,但是:

print row[0].value.decode('utf-8')

也无济于事:

Traceback (most recent call last):
  File "xls_import.py", line 8, in <module>
    print row[0].value.decode('utf-8')
  File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

有什么建议吗?

我是 运行 Python 2.7 和 openpyxl 2.2.5。

openpyxl returns unicode 字符串(XML 本身是用 UTF-8 编码的)所以你不需要 decode 它们(解码来自unicode 的编码)但是 encode 它们在您选择的编码中。