如何在 python 中以 tabula-py 数据格式循环

How to loop in tabula-py data format in python

我想知道如何从 python 中的 pdf 文件中提取特定的 table 列。

到目前为止我的代码

    import tabula.io as tb
from tabula.io import read_pdf
dfs = tb.read_pdf(pdf_path, pages='all')
print (len(dfs)) [It displays 73]

我可以通过打印 (dfs[2]['Section ID']) 访问单个 table 列 我想知道如何使用 for 循环在所有数据框中搜索特定列。

我想做这样的事情

for i in range(len(dfs)):
    if (dfs[i][2]) == 'Section ID ' //(This gives invalid syntax)
    print dfs[i]

如果您只有一个名称为 Section ID 的数据框(或者只对包含此列的第一个数据框感兴趣),您可以遍历 read_pdf 返回的列表,检查列是否存在当找到匹配项时使用 in df.columnsbreak

import tabula.io as tb
from tabula.io import read_pdf
df_list = tb.read_pdf(pdf_path, pages='all')

for df in df_list:
    if 'Section ID' in df.columns:
        break
print(df)

如果您可能有多个包含 Section ID 列的数据框,您可以使用列表理解过滤器并获得包含该列名称的数据框的 list

dfs_section_id = [df for df in df_list if 'Section ID' in df.columns]