使用 Python 逐块加载 Excel 文件,而不是将整个文件加载到内存中
Loading Excel file chunk by chunk with Python instead of loading full file into memory
我只想从 Excel 个文件 (xlsx) 中读取 10 行,而不是一次加载整个文件,因为它不能在我的一台机器上完成(内存不足)。
我尝试使用
import xlrd
import pandas as pd
def open_file(path):
xl = pd.ExcelFile(path)
reader = xl.parse(chunksize=1000)
for chunk in reader:
print(chunk)
好像是先加载文件,然后分成几个部分。
如何只读第一行?
由于 xlsx
文件的性质(本质上是一堆压缩在一起的 xml
文件),您不能以任意字节戳文件并希望它是您感兴趣的 sheet 中 table 的第 N 行的开头。
最好的办法是将 pandas.read_excel
与 skiprows
(从文件顶部跳过行)和 skip_footer
(从底部跳过行)参数一起使用。然而,这将首先将整个文件加载到内存中,然后仅解析所需的行。
# if the file contains 300 rows, this will read the middle 100
df = pd.read_excel('/path/excel.xlsx', skiprows=100, skip_footer=100,
names=['col_a', 'col_b'])
请注意,您必须使用 names
参数手动设置 headers,否则列名将是最后跳过的行。
如果您想使用 csv
,那么这是一项简单的任务,因为 csv
个文件是 plain-text 个文件。
But,而且是一个很大的but,如果你真的很绝望,你可以提取相关的sheet xlsx
档案中的 xml
文件并解析它。但这不是一件容易的事。
一个 xml
文件示例,它代表一个 sheet 和一个 2 X 3 table。 <v>
标签表示单元格的值。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
<dimension ref="A1:B3"/>
<sheetViews>
<sheetView tabSelected="1" workbookViewId="0">
<selection activeCell="C10" sqref="C10"/>
</sheetView>
</sheetViews>
<sheetFormatPr defaultColWidth="11" defaultRowHeight="14.25" x14ac:dyDescent="0.2"/>
<sheetData>
<row r="1" spans="1:2" ht="15.75" x14ac:dyDescent="0.2">
<c r="A1" t="s">
<v>1</v>
</c><c r="B1" s="1" t="s">
<v>0</v>
</c>
</row>
<row r="2" spans="1:2" ht="15" x14ac:dyDescent="0.2">
<c r="A2" s="2">
<v>1</v>
</c><c r="B2" s="2">
<v>4</v>
</c>
</row>
<row r="3" spans="1:2" ht="15" x14ac:dyDescent="0.2">
<c r="A3" s="2">
<v>2</v>
</c><c r="B3" s="2">
<v>5</v>
</c>
</row>
</sheetData>
<pageMargins left="0.75" right="0.75" top="1" bottom="1" header="0.5" footer="0.5"/>
</worksheet>
我只想从 Excel 个文件 (xlsx) 中读取 10 行,而不是一次加载整个文件,因为它不能在我的一台机器上完成(内存不足)。
我尝试使用
import xlrd
import pandas as pd
def open_file(path):
xl = pd.ExcelFile(path)
reader = xl.parse(chunksize=1000)
for chunk in reader:
print(chunk)
好像是先加载文件,然后分成几个部分。
如何只读第一行?
由于 xlsx
文件的性质(本质上是一堆压缩在一起的 xml
文件),您不能以任意字节戳文件并希望它是您感兴趣的 sheet 中 table 的第 N 行的开头。
最好的办法是将 pandas.read_excel
与 skiprows
(从文件顶部跳过行)和 skip_footer
(从底部跳过行)参数一起使用。然而,这将首先将整个文件加载到内存中,然后仅解析所需的行。
# if the file contains 300 rows, this will read the middle 100
df = pd.read_excel('/path/excel.xlsx', skiprows=100, skip_footer=100,
names=['col_a', 'col_b'])
请注意,您必须使用 names
参数手动设置 headers,否则列名将是最后跳过的行。
如果您想使用 csv
,那么这是一项简单的任务,因为 csv
个文件是 plain-text 个文件。
But,而且是一个很大的but,如果你真的很绝望,你可以提取相关的sheet xlsx
档案中的 xml
文件并解析它。但这不是一件容易的事。
一个 xml
文件示例,它代表一个 sheet 和一个 2 X 3 table。 <v>
标签表示单元格的值。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
<dimension ref="A1:B3"/>
<sheetViews>
<sheetView tabSelected="1" workbookViewId="0">
<selection activeCell="C10" sqref="C10"/>
</sheetView>
</sheetViews>
<sheetFormatPr defaultColWidth="11" defaultRowHeight="14.25" x14ac:dyDescent="0.2"/>
<sheetData>
<row r="1" spans="1:2" ht="15.75" x14ac:dyDescent="0.2">
<c r="A1" t="s">
<v>1</v>
</c><c r="B1" s="1" t="s">
<v>0</v>
</c>
</row>
<row r="2" spans="1:2" ht="15" x14ac:dyDescent="0.2">
<c r="A2" s="2">
<v>1</v>
</c><c r="B2" s="2">
<v>4</v>
</c>
</row>
<row r="3" spans="1:2" ht="15" x14ac:dyDescent="0.2">
<c r="A3" s="2">
<v>2</v>
</c><c r="B3" s="2">
<v>5</v>
</c>
</row>
</sheetData>
<pageMargins left="0.75" right="0.75" top="1" bottom="1" header="0.5" footer="0.5"/>
</worksheet>