簡體   English   中英

嘗試使用 openpyxl 操作 xlsx 文件時出現 UnicodeDecodeError

[英]UnicodeDecodeError when trying to operate xlsx file with openpyxl

我正在嘗試讓我的程序從 xlsx 文件中導入數據。 這是代碼的一部分:

def Volunteer_list_show():
    volunteer_list = load_workbook('Volunteer_list.xlsx')
    surnames = volunteer_list['A']
    names = volunteer_list['B']
    ...
    identificator = volunteer_list['K']

並有這種類型(UnicodeDecodeError)的錯誤信息:

UnicodeDecodeError:“utf-8”編解碼器無法解碼 position 中的字節 0xec 1:無效的繼續字節

我從其他線程嘗試了這個解決方案

def Volunteer_list_encoding_check(volunteer_list):
    encodings = ['utf-8', 'iso-8859-1', 'windows-1251', 'windows-1252']
    for encoding in encodings:
        try:
            decoded_file = volunteer_list.decode(encoding)
        except UnicodeDecodeError:
            pass
        else:
            decoded_file = decoded_file.encode("utf-8")
            break
    else:
        print('Not approved encoding')  

但是,即使刪除了def Volunteer_list_show() ,我也遇到了與第一次相同的錯誤。 單獨通過load_workbook function 打開工作簿不會導致問題。 其他代碼檢查並不會導致問題。

如何在 UTF-8 中解碼編碼 xlsx?

我發現了我的錯誤:您必須使用活動工作表來獲取單元格或其值,所以我剛剛添加了以下代碼:

volunteer_list_act = volunteer_list.active

現在可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM