我不熟悉python和pandas,正在尝试将pdf文档转换为panda数据帧
我能够将pdf提取到文本中,但不确定如何将文本文件中的以下所需值保存到字典中
到目前为止,我已使用以下代码将pdf提取为文本,如下所示:
import pandas as pd
import pdfplumber
data = './data/coc.pdf'
with pdfplumber.open(data) as pdf:
page = pdf.pages[0]
text = page.extract_text()
print(text)
下面是文本文件
" \n \nCertificate of Currency \nXYZ Limited \nABN 121011100 54720 AFSL 232111 \n \nAs at Date \n2 November 2015 \nPolicy Information \nPolicy Type \nProfessional \n \n \nInsured \nUniversity of ABC and others as defined by the policy \ndocument. \n \nInsurer \nMMO Limited \n \nPolicy Number(s) \L0K107721013 \n \nPeriod of Insurance \nFrom 4.00pm 1/11/2015 to 4.00pm 1/11/2016 \n \nInterest Insured \nLoss incurred as a result of a civil liability claim made against the insured \nbased solely on the insured’s provision of their professional services \n \nLimit of Liability \n$20,000,000 any one claim and $60,000,000 in the aggregate for all claims "
现在我想把文本文件转换成字典。如何使用if条件更新下面的代码以创建一个字典,该字典将基于\n或\n\n的key:value对
d = dict([i.splitlines() for i in text.split('\n\n')])
我得到以下错误:
错误:字典更新序列元素:
请查看随附的所需输出表:
期望输出:
更新的答案
我做了一些改变,使它更容易做,并使它成为如何真正找到价值观
输出
注意:前面的答案
此答案适用于问题的上一次迭代:-/
您需要处理文本中的行,并找到正确的行来提取所需的数据
我提供了一些例子,说明如何找到问题中列出的值。我建议您检查一下我使用的基本字符串方法
string.split()
string.splitlines()
string.strip()
f-strings (I really like f-strings)
下面的代码应该足以让您开始
代码
输出
您的问题确实有点广泛,但如果我理解正确,您的主要问题是将PDF中的表转换为Pandas dataframe,并且在将PDF作为文本阅读后,您制定了问题中的问题。然而,我认为只使用tabla更容易,让它直接将PDF解析为dataframe
安装表格:
使用表格:
就这样。您的其他问题与Pandas有关,我建议您在Pandas文档/论坛中搜索答案,或者就如何处理数据帧提出单独的问题
请注意,PDF处理从来都不是100%准确的,因为PDF本质上不是由机器解释的。此解决方案可能有效,也可能无效,具体取决于PDF。对我来说,它确实解决了类似的问题
相关问题 更多 >
编程相关推荐