起初尝试读取 excel/csv 数据以在插入数据库之前对其进行检查,虽然 csv 工作正常可以读取数据,但 xlsx 和 xlx 显示以下错误
/academy/add_advisor 'utf-8' 的 UnicodeDecodeError 编解码器无法解码位置 10 中的字节 0xa1:起始字节无效
我的代码片段:
from tablib import Dataset
this_file = request.FILES['bulk_file']
dataset = Dataset()
imported_data = dataset.load(this_file.read().decode("utf-8"),format='xlsx')
for data in dataset:
print(data[0], data[1], data[2], data[3], data[4])
根据来自此行的错误消息错误
imported_data = dataset.load(this_file.read().decode("utf-8"),format='xlsx')
我试图导入的 excel 文件,我从谷歌驱动器 excel 下载它作为 xlsx 文件(microsoft excel)。还从 onedrive(microsoft) xlsx 文件下载了一个,仍然出现相同的错误。
我尝试过的其他几种方法是
imported_data = dataset.load(this_file.read().decode("ISO-8859-1"),format='xlsx')
imported_data = dataset.load(this_file.read().strip().decode("ISO-8859-1"),format='xlsx')
imported_data = dataset.load(this_file.read().strip().decode("CP1252"),format='xlsx')
imported_data = dataset.load(this_file.read().strip().decode("windows-1252"),format='xlsx')
imported_data = dataset.load(this_file.read().strip().decode("Latin-1"),format='xlsx')
但没有运气:(如果有更好的方法我可以尝试请分享:)(:谢谢你的阅读:)