【发布时间】:2016-06-15 03:38:54
【问题描述】:
我正在尝试使用 csv 模块读取具有 .xlsx 格式的 excel 文件,但在使用 excel 文件时,即使指定了我的方言和编码,我也没有任何运气。下面,我展示了我尝试的不同编码的不同尝试和错误结果。如果有人能指出我可以用来在 Python 中读取 .xlsx 文件的正确编码、语法或模块,我将不胜感激。
使用以下代码,我收到以下错误:_csv.Error: line contains NULL byte
#!/usr/bin/python
import sys, csv
with open('filelocation.xlsx', "r+", encoding="Latin1") as inputFile:
csvReader = csv.reader(inputFile, dialect='excel')
for row in csvReader:
print(row)
使用以下代码,我收到以下错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 16: invalid continuation byte
#!/usr/bin/python
import sys, csv
with open('filelocation.xlsx', "r+", encoding="Latin1") as inputFile:
csvReader = csv.reader(inputFile, dialect='excel')
for row in csvReader:
print(row)
当我在encoding 中使用utf-16 时,出现以下错误:UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 570-571: illegal UTF-16 surrogate
【问题讨论】:
-
您无法使用 csv 模块读取 xlsx 文件。 Excel 方言仅表示您可以读取使用 Excel 创建的 CSV 文件。
-
你知道有哪些模块可以读取 .xlsx 文件吗?
-
您可以使用 Google 搜索找到很多模块,但您应该测试它们是否适合您的用例。
-
问题的编辑是无可挑剔的,它真的帮助了我,即使没有阅读任何答案。谢谢。
标签: python excel encoding utf-8