gpt4 book ai didi

python - Tabula-py 没有正确拆分列

转载 作者:太空宇宙 更新时间:2023-11-03 11:18:48 29 4
gpt4 key购买 nike

我刚刚发现了 tabula-py(当然还有 tabula-java)从 pdf 中提取表格的乐趣。我现在正在为我的工作编写一个脚本,该脚本从 pdf 表中读取一些数据,对其进行一些清理,然后将其导出到 excel 中。我用的pdf每天都是一样的格式,表格总是在某个区域。为了检测该区域,我使用了 tabula.exe:我选择表格,可视化预览(看起来不错),然后导出脚本,以便查看 tabula.exe 使用的 -a 参数。然后我在 Python 的命令中使用它,即:

df = tabula.read_pdf(os.fsdecode(directory)+filename, encoding = 'ISO-8859-1',
stream=True, area = "81.106,302.475,384.697,552.491", pages = 2, pandas_options={'header':None})

我使用编码参数是因为标准 utf-8 会返回错误,我使用流方法是因为它在 tabula.exe 中显示了一个很好的提取表。但是,数据框有一个问题,因为前 2 列(在 tabula.exe 的预览中正确显示为 2 个不同的列)实际上是一个单独的列,因此名称和值混合在一起。

您是否知道为什么同一区域在 tabula-py 和 tabula.exe 中会产生 2 个不同的结果?非常感谢!

最佳答案

在 GitHub 上弄明白了:tabula-py 的“猜测”选项默认设置为 True。因此,要更正差异,只需添加 guess=False,输出将相同!

    df = tabula.read_pdf(os.fsdecode(directory)+filename, encoding = 'ISO-8859-1', 
stream=True, area = "81.106,302.475,384.697,552.491", pages = 2, guess = False, pandas_options={'header':None})

关于python - Tabula-py 没有正确拆分列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47357172/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com