当尝试用openpyxl解析.xlsx时,“UnicodeEncodeError:‘charmap’编解码器不能编码字符”

时间:2021-04-03 21:51:59

--- update ---

推荐- - - - - - - - - - - -更新

I think this console log nails the issue, however it's still not clear how to fix it:

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

>>> 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>

--- end update ---

- - -更新——结束

I'm trying to print the values of some .xlsx cells using openpyxl:

我正在尝试使用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

Which results in the following error:

导致以下错误:

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>

As far as I know, XLSX is encoded as UTF-8, however:

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

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

does not help either:

不能帮助:

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)

Any suggestions?

有什么建议吗?

I'm running Python 2.7 and openpyxl 2.2.5.

运行的是Python 2。7和openpyxl 2。2。5。

1 个解决方案

#1


1  

openpyxl returns unicode strings (XML itself is encoded in UTF-8) so you don't need to decode them (decoding goes from an encoding to unicode) but encode them in encoding of your choice.

openpyxl返回unicode字符串(XML本身编码在UTF-8中),所以您不需要解码它们(解码从编码到unicode),但是编码它们编码您的选择。

#1


1  

openpyxl returns unicode strings (XML itself is encoded in UTF-8) so you don't need to decode them (decoding goes from an encoding to unicode) but encode them in encoding of your choice.

openpyxl返回unicode字符串(XML本身编码在UTF-8中),所以您不需要解码它们(解码从编码到unicode),但是编码它们编码您的选择。