在 python 中下载和使用 xls 文件时出现问题

Problems downloading and using xls file in python

我正在尝试使用 urllib 和 xlrd 下载和操作 xls 文件。

数据来自urlhttp://profiles.doe.mass.edu/search/search_export.aspx?orgCode=&orgType=5,12&runOrgSearch=Y&searchType=ORG&leftNavId=11238&showEmail=N

我正在使用 Python 2.7、xlrd 0.9.4、urllib 1.17,并且我在 Mac.

我能够使用此代码成功下载文件。

saveLocation = home_dir+"/test/"
fileName = "data.xls"
page = <the url given above>
urllib.urlretrieve(page, saveLocation+fileName)

然后我尝试使用 xlrd 打开文件

wb = xlrd.open_workbook(saveLocation+fileName)

但是报错

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '\r\n\r\n<htm' 

这告诉我文件没有作为真正的 xls 文件下载。 我可以在 Excel 中打开文件并且没有弹出警告或兼容性错误。 奇怪的是,如果我随后将文件(在 Excel 中)保存为 Excel 97-2004,xlrd 错误就会消失。所以看起来 Excel "fixes" 文件出了什么问题。

所以我的问题是,如何 "fix" python 中的文件或以 xlrd 能够识别的适当格式下载数据?

我也尝试过将文件下载为 xlsx 文件并使用 openpyxl,但出现了类似的错误。 openpyxl 说它不是有效的 zip 文件。 我也尝试过使用请求等不同方法下载数据。

谢谢。

编辑: 使用@DSM 提供的信息,我能够下载并使用 Excel 文件。这是我使用的代码。

dfs = pd.read_html(fileLocation+fileName, index_col = 7, header=0)[0]
writer = pd.ExcelWriter(fileLocation+fileName)
dfs.to_excel(writer,"Sheet1")
writer.save()

然后我能够将文件作为真正的 Excel 文件进行访问

ws = pd.read_excel(fileLocation+fileName, 0) 

正如 <htm 位所暗示的那样,尽管名称为 .xls,这实际上是以 xml 方式呈现的数据。 (几乎总是值得在您最喜欢的编辑器中手动查看数据 header 以检查实际内容是什么,但结果却难以阅读。)有时这可能是一个真正的麻烦处理,但是幸运的是,我们可以简单地使用 read_html:

来阅读它
>>> url="http://profiles.doe.mass.edu/search/search_export.aspx?orgCode=&orgType=5,12&runOrgSearch=Y&searchType=ORG&leftNavId=11238&showEmail=N"
>>> dfs = pd.read_html(url)
>>> len(dfs)
1
>>> dfs[0].iloc[:5,:5]
                                                   0         1  \
0                                           Org Name  Org Code   
1       Abby Kelley Foster Charter Public (District)  04450000   
2                                           Abington  00010000   
3  Academy Of the Pacific Rim Charter Public (Dis...  04120000   
4                                     Acton (non-op)  00020000   

                        2                      3              4  
0                Org Type               Function   Contact Name  
1        Charter District  Charter School Leader     Brian Haas  
2  Public School District         Superintendent  Peter Schafer  
3        Charter District  Charter School Leader  Chris Collins  
4  Public School District         Superintendent    Glenn Brand  

仔细观察,我们发现我们可以将第 0 行用于 headers,因此:

>>> df = pd.read_html(url, header=0)[0]
>>> df.iloc[:5, :5]
                                            Org Name  Org Code  \
0       Abby Kelley Foster Charter Public (District)   4450000   
1                                           Abington     10000   
2  Academy Of the Pacific Rim Charter Public (Dis...   4120000   
3                                     Acton (non-op)     20000   
4                                   Acton-Boxborough   6000000   

                 Org Type               Function   Contact Name  
0        Charter District  Charter School Leader     Brian Haas  
1  Public School District         Superintendent  Peter Schafer  
2        Charter District  Charter School Leader  Chris Collins  
3  Public School District         Superintendent    Glenn Brand  
4  Public School District         Superintendent    Glenn Brand