【问题标题】:Python Openpyxl Language RussianPython Openpyxl 语言 俄语
【发布时间】:2017-03-27 07:53:48
【问题描述】:

我在提取和打印俄语字符串时遇到问题

使用 Python 2.7 和 Lib Openpyxl

代码使用是:

workbook = load_workbook(filename ='sample.xlsx')
first_sheet = workbook.get_sheet_names()[0]
worksheet = workbook.get_sheet_by_name(first_sheet)

ws = workbook.active

riga = 0;
id_cell = 0;
lenght_word = 0;

for row in worksheet.iter_rows():
    riga+=1
    id_cell=0;
    if riga > 4:
        for cell in row:
            id_cell+=1
            if id_cell == 3:
                lenght_word = cell.value
                print lenght_word
            if id_cell > 3:   
                try:
                    #print cell
                    str_lenght = len(cell.value)
                    print cell.value, str_lenght

打印时产生错误 返回 codecs.charmap_encode(input,errors,encoding_map)

谢谢

【问题讨论】:

  • 使用win7,STDOUT格式为cp850

标签: python openpyxl translate cyrillic


【解决方案1】:

问题在于您的打印语句和 Python 2。所有非数字单元格值都是 unicode,因此您的代码必须管理从 unicode 到适合您系统的编码的转换。

【讨论】:

  • 但我使用的是代码:stringa = cell.value stru = stringa.encode('utf-8') 查看与原始字符串不同的字符串
  • 那又怎样?当您读取该值时,您将始终获得 unicode。
猜你喜欢
  • 1970-01-01
  • 2015-09-06
  • 2021-12-14
  • 1970-01-01
  • 2019-07-16
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多