读取Excel时出现Pandas的AssertionError [英] AssertionError with pandas when reading excel
问题描述
我正在尝试使用熊猫将xlsx文件读取到python中.
我已经做过数千次了,但是由于某些原因它不能用于特定文件.
I'm trying to read an xlsx file into python using pandas.
I've done this thousands of times before but some reason it is not working with a particular file.
该文件是从其他来源下载的,使用熊猫阅读时会出现AssertionError(参见结尾):
The file is downloaded from another source and I get an AssertionError (see end) when reading with pandas:
df = pandas.read_excel(pathtomyfile, sheetname = "Sheet1")
为路径定义了变量.路径存在(os.path.exists(path)返回True).
The variable is defined for the path. The path exists (os.path.exists(path) returns True).
当我复制文件的内容并将其值粘贴到新的excel文档中时,将使用read_excel()方法打开此新文档.
When I copy the contents of the file and paste the values in a new excel doc, this new one will open with the read_excel() method.
当我复制文件内容并将格式粘贴到新的Excel中时, 这一新功能将使用read_excel()方法打开.
When I copy the contents of the file and paste the formatting in a new excel, this new one will open with the read_excel() method.
似乎不是值或格式.
我猜这可能是编码问题?
谢谢您的帮助.
I am guessing this could be an encoding issue?
Thank you for any help.
df1 = pandas.read_excel(snap1)
File "C:\Python\python-3.4.4.amd64\lib\site-packages\pandas\io\excel.py", line 163, in read_excel
io = ExcelFile(io, engine=engine)
File "C:\Python\python-3.4.4.amd64\lib\site-packages\pandas\io\excel.py", line 206, in __init__
self.book = xlrd.open_workbook(io)
File "C:\Python\python-3.4.4.amd64\lib\site-packages\xlrd\__init__.py", line 422, in open_workbook
ragged_rows=ragged_rows,
File "C:\Python\python-3.4.4.amd64\lib\site-packages\xlrd\xlsx.py", line 794, in open_workbook_2007_xml
x12sheet.process_stream(zflo, heading)
File "C:\Python\python-3.4.4.amd64\lib\site-packages\xlrd\xlsx.py", line 531, in own_process_stream
self_do_row(elem)
File "C:\Python\python-3.4.4.amd64\lib\site-packages\xlrd\xlsx.py", line 597, in do_row
assert 0 <= self.rowx < X12_MAX_ROWS
AssertionError
推荐答案
文件中包含韩语字符.这些需要替代编码. 在read_excel()方法中使用"encoding"参数可以解决该问题.
The file contained Korean characters in the text. These needed alternative encoding. Using the "encoding" parameter in the read_excel() method resolved the issue.
df = pandas.read_excel(路径文件,工作表名称为"Sheet1",编码为"utf-16")
df = pandas.read_excel(pathtomyfile, sheetname = "Sheet1", encoding="utf-16")
这篇关于读取Excel时出现Pandas的AssertionError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!