我正在尝试将一个
xls
文件(只有一个选项卡)打开到一个熊猫数据框中。
这是一个我通常可以在excel或excel中读取的文件,实际上这里是原始文件: https://www.dropbox.com/scl/fi/zbxg8ymjp8zxo6k4an4dj/product-screener.xls?dl=0&rlkey=3aw7whab78jeexbdkthkjzkmu 。
我注意到前两行合并了单元格,一些列也合并了。
我尝试过几种方法(从堆栈),但都失败了。
# method 1 - read excel
file = "C:\\Users\\admin\\Downloads\\product-screener.xls"
df = pd.read_excel(file)
print(df)
错误:
Excel file format cannot be determined, you must specify an engine manually.
# method 2 - pip install xlrd and use engine
file = "C:\\Users\\admin\\Downloads\\product-screener.xls"
df = pd.read_excel(file, engine='xlrd')
print(df)
错误:
Unsupported format, or corrupt file: Expected BOF record; found b'\xef\xbb\xbf<?xml'
# method 3 - rename to xlsx and open with openpyxl
file = "C:\\Users\\admin\\Downloads\\product-screener.xlsx"
df = pd.read_excel(file, engine='openpyxl')
print(df)
错误:
File is not a zip file
(可能转换,而不是重命名,是一个选项)。
# method 4 - use read_xml
file = "C:\\Users\\admin\\Downloads\\product-screener.xls"
df = pd.read_xml(file)
print(df)
这个方法实际上产生了一个结果,但是产生了一个与工作表无关的DataFrame。想必人们需要解释xml (似乎很复杂)?
Style Name Table
0 NaN None NaN
1 NaN All funds NaN
# method 5 - use read_table
file = "C:\\Users\\admin\\Downloads\\product-screener.xls"
df = pd.read_table(file)
print(df)
此方法将文件读入一个列(系列) DataFrame中。那么,如何使用这些信息创建与xls文件形状相同的标准2d DataFrame呢?
0 <Workbook xmlns="urn:schemas-microsoft-com:off...
1 <Styles>
2 <Style ss:ID="Default">
3 <Alignment Horizontal="Left"/>
4 </Style>
... ...
226532 </Cell>
226533 </Row>
226534 </Table>
226535 </Worksheet>
226536 </Workbook>
# method 5 - use read_html
file = "C:\\Users\\admin\\Downloads\\product-screener.xls"
df = pd.read_html(file)
print(df)
这将返回一个空列表
[]
,而可能至少会有一个DataFrames列表。
因此,问题是,将该文件读取为dataframe (或类似的可用格式)的最简单方法是什么?
发布于 2022-02-02 15:36:45
不是一个完整的解决方案,但它应该让你开始。
"xls"
文件实际上是
SpreadsheetML
格式的普通
xml
文件。将文件扩展名更改为
.xml
--在internet浏览器中查看它,结构(至少是给定文件的结构)相当简单。
以下是将数据内容读入熊猫DataFrame中:
import pandas as pd
import xml.etree.ElementTree as ET
tree = ET.parse('product-screener.xls')
root = tree.getroot()
data = [[c[0].text for c in r] for r in root[1][0][2:]]
types = [c[0].get('{urn:schemas-microsoft-com:office:spreadsheet}Type') for c in root[1][0][2]]
df = pd.DataFrame(data)