繁体   English   中英

从非结构化表中提取特定信息。 如何修复此错误并构建我的数据并获得所需的表?

[英]extract specific information from unstructured table. How can I fix this error and structure my data and get my desired table?

我已将我的 pdf 文件转换为 excel,现在我得到了这样的表格

你的图片描述在这里

我想从表格中提取信息,如“姓名”、“考试编号”、“身份证”、“考试地址”、“理论考试时间”和“技能考试时间”。 并将这些信息写入一个新的 excel 文件。 有谁知道我如何用 Python 做到这一点?

我尝试使用 pandas 的 dataraframe

import pandas as pd

df = pd.read_excel (r’Copy of pdf-v1.xlsx')
print (df)

但是数据框非常非结构化,并且“考试 ID”和人名分布在桌子上的各个地方。 像这样

0                                             Exam ID   
1                                                  ID   
2                                             Company   
3                                                 Job   
4                              subject of examination   
5                                             Address   
6                                    theory test time   
7                                  Skill test address   
8                                     Skill test time   
9                                                 NaN   
10                                                NaN   
11                      P.S.: \n1. xxxx\n2. dfr\n3.\n   
12  examination                                   ...   
13                                               name   
14                                            Exam ID   
15                                                 ID   
16                                            Company   
17                                                Job   
18                             subject of examination   
19                                            Address   
20                                   theory test time   
21                                 Skill test address   
22                                    Skill test time   
23                                                NaN   
24                                                NaN   
25                      P.S.: \n1. xxxx\n2. dfr\n3.\n   
26  examination                                   ...   
27                                               name   
28                                            Exam ID   
29                                                 ID   
30                                            Company   
31                                                Job   
32                             subject of examination   
33                                            Address   
34                                   theory test time   
35                                 Skill test address   
36                                    Skill test time   
37                                                NaN   
38                                                NaN   
39                      P.S.: \n1. xxxx\n2. dfr\n3.\n   
40  examination                                   ...   
41                                               name   
42                                            Exam ID   
43                                                 ID   
44                                            Company   
45                                                Job   
46                             subject of examination   
47                                            Address   
48                                   theory test time   
49                                 Skill test address   
50                                    Skill test time   
51                                                NaN   
52                                                NaN   
53                      P.S.: \n1. xxxx\n2. dfr\n3.\n   

                                Smith  gender     male   Unnamed: 4  \
0                               78610      NaN     NaN          NaN   
1                           108579352      NaN     NaN          NaN   
2                                 NaN      NaN     NaN          NaN   
3                              Police      NaN   Level   Level Five   
4                       Theory, Skill      NaN  Degree  high school   
5                               dffef      NaN     NaN          NaN   
6   2021-10-18 10:00~2021-10-18 11:30      NaN     NaN          NaN   
7                            cdwgrgtr      NaN     NaN          NaN   
8                    2021-10-19 09:30      NaN     NaN          NaN   
9                                 NaN      NaN     NaN          NaN   
10                                NaN      NaN     NaN          NaN   
11                                NaN      NaN     NaN          NaN   
12                                NaN      NaN     NaN          NaN   
13                            Charles  gender     male          NaN   
14                              74308      NaN     NaN          NaN   
15                          733440627      NaN     NaN          NaN   
16                                NaN      NaN     NaN          NaN   
17                           engeneer      NaN   Level   Level Five   
18                      Theory, Skill      NaN  Degree       Master   
19                              dffef      NaN     NaN          NaN   
20  2021-10-18 10:00~2021-10-18 11:30      NaN     NaN          NaN   
21                           cdwgrgtr      NaN     NaN          NaN   
22                   2021-10-19 09:30      NaN     NaN          NaN   
23                                NaN      NaN     NaN          NaN   
24                                NaN      NaN     NaN          NaN   
25                                NaN      NaN     NaN          NaN   
26                                NaN      NaN     NaN          NaN   
27                               Gary  gender     male          NaN   
28                              77564      NaN     NaN          NaN   
29                          392096759      NaN     NaN          NaN   
30                                NaN      NaN     NaN          NaN   
31                             driver      NaN   Level   Level Five   
32                      Theory, Skill      NaN  Degree  High school   
33                              dffef      NaN     NaN          NaN   
34  2021-10-18 10:00~2021-10-18 11:30      NaN     NaN          NaN   
35                           cdwgrgtr      NaN     NaN          NaN   
36                   2021-10-19 09:30      NaN     NaN          NaN   
37                                NaN      NaN     NaN          NaN   
38                                NaN      NaN     NaN          NaN   
39                                NaN      NaN     NaN          NaN   
40                                NaN      NaN     NaN          NaN   
41                            Whitney  gender   female          NaN   
42                              78853      NaN     NaN          NaN   
43                          207628593      NaN     NaN          NaN   
44                                NaN      NaN     NaN          NaN   
45                    data scientist       NaN   Level   Level Five   
46                      Theory, Skill      NaN  Degree       Master   
47                              dffef      NaN     NaN          NaN   
48  2021-10-18 10:00~2021-10-18 11:30      NaN     NaN          NaN   
49                           cdwgrgtr      NaN     NaN          NaN   
50                   2021-10-19 09:30      NaN     NaN          NaN   
51                                NaN      NaN     NaN          NaN   
52                                NaN      NaN     NaN          NaN   
53                                NaN      NaN     NaN          NaN   

    Unnamed: 5  Unnamed: 6  Unnamed: 7                         name.1  \
0          NaN         NaN         NaN                        Exam ID   
1          NaN         NaN         NaN                             ID   
2          NaN         NaN         NaN                        Company   
3          NaN         NaN         NaN                            Job   
4          NaN         NaN         NaN         subject of examination   
5          NaN         NaN         NaN                        Address   
6          NaN         NaN         NaN               theory test time   
7          NaN         NaN         NaN             Skill test address   
8          NaN         NaN         NaN                Skill test time   
9          NaN         NaN         NaN                            NaN   
10         NaN         NaN         NaN                            NaN   
11         NaN         NaN         NaN  P.S.: \n1. xxxx\n2. dfr\n3.\n   
12         NaN         NaN         NaN                            NaN   
13         NaN         NaN         NaN                           name   
14         NaN         NaN         NaN                        Exam ID   
15         NaN         NaN         NaN                             ID   
16         NaN         NaN         NaN                        Company   
17         NaN         NaN         NaN                            Job   
18         NaN         NaN         NaN         subject of examination   
19         NaN         NaN         NaN                        Address   
20         NaN         NaN         NaN               theory test time   
21         NaN         NaN         NaN             Skill test address   
22         NaN         NaN         NaN                Skill test time   
23         NaN         NaN         NaN                            NaN   
24         NaN         NaN         NaN                            NaN   
25         NaN         NaN         NaN  P.S.: \n1. xxxx\n2. dfr\n3.\n   
26         NaN         NaN         NaN                            NaN   
27         NaN         NaN         NaN                           name   
28         NaN         NaN         NaN                        Exam ID   
29         NaN         NaN         NaN                             ID   
30         NaN         NaN         NaN                        Company   
31         NaN         NaN         NaN                            Job   
32         NaN         NaN         NaN         subject of examination   
33         NaN         NaN         NaN                        Address   
34         NaN         NaN         NaN               theory test time   
35         NaN         NaN         NaN             Skill test address   
36         NaN         NaN         NaN                Skill test time   
37         NaN         NaN         NaN                            NaN   
38         NaN         NaN         NaN                            NaN   
39         NaN         NaN         NaN  P.S.: \n1. xxxx\n2. dfr\n3.\n   
40         NaN         NaN         NaN                            NaN   
41         NaN         NaN         NaN                           name   
42         NaN         NaN         NaN                        Exam ID   
43         NaN         NaN         NaN                             ID   
44         NaN         NaN         NaN                        Company   
45         NaN         NaN         NaN                            Job   
46         NaN         NaN         NaN         subject of examination   
47         NaN         NaN         NaN                        Address   
48         NaN         NaN         NaN               theory test time   
49         NaN         NaN         NaN             Skill test address   
50         NaN         NaN         NaN                Skill test time   
51         NaN         NaN         NaN                            NaN   
52         NaN         NaN         NaN                            NaN   
53         NaN         NaN         NaN  P.S.: \n1. xxxx\n2. dfr\n3.\n   

                               Philip gender .1  male.1  Unnamed: 12  
0                               80425       NaN     NaN          NaN  
1                           121246038       NaN     NaN          NaN  
2                                 NaN       NaN     NaN          NaN  
3                              driver       NaN   Level   Level Five  
4                       Theory, Skill       NaN  Degree     Bachelor  
5                               dffef       NaN     NaN          NaN  
6   2021-10-18 10:00~2021-10-18 11:30       NaN     NaN          NaN  
7                            cdwgrgtr       NaN     NaN          NaN  
8                    2021-10-19 09:30       NaN     NaN          NaN  
9                                 NaN       NaN     NaN          NaN  
10                                NaN       NaN     NaN          NaN  
11                                NaN       NaN     NaN          NaN  
12                                NaN       NaN     NaN          NaN  
13                               Bill   gender     male          NaN  
14                              74788       NaN     NaN          NaN  
15                          332094931       NaN     NaN          NaN  
16                                NaN       NaN     NaN          NaN  
17                             driver       NaN   Level   Level Five  
18                      Theory, Skill       NaN  Degree  High school  
19                              dffef       NaN     NaN          NaN  
20  2021-10-18 10:00~2021-10-18 11:30       NaN     NaN          NaN  
21                           cdwgrgtr       NaN     NaN          NaN  
22                   2021-10-19 09:30       NaN     NaN          NaN  
23                                NaN       NaN     NaN          NaN  
24                                NaN       NaN     NaN          NaN  
25                                NaN       NaN     NaN          NaN  
26                                NaN       NaN     NaN          NaN  
27                              Betty   gender   female          NaN  
28                              61311       NaN     NaN          NaN  

此外,我尝试读取标签并提取下一列单元格以获得所需的结果,但循环也会出现错误,代码如下:

for index, row in df.iterrows():
    if row['A2'] == 'Exam Id':
        exam_id=row['B2']

然后python说关键错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3360             try:
-> 3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:

~/opt/anaconda3/lib/python3.7/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

~/opt/anaconda3/lib/python3.7/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'A2'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-36-03e11799668c> in <module>
      1 for index, row in df.iterrows():
----> 2     if row['A2'] == 'Exam Id':
      3         exam_id=row['B2']

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in __getitem__(self, key)
    940 
    941         elif key_is_scalar:
--> 942             return self._get_value(key)
    943 
    944         if is_hashable(key):

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in _get_value(self, label, takeable)
   1049 
   1050         # Similar to Index.get_value, but we do not fall back to positional
-> 1051         loc = self.index.get_loc(label)
   1052         return self.index._get_values_for_loc(self, loc, label)
   1053 

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:
-> 3363                 raise KeyError(key) from err
   3364 
   3365         if is_scalar(key) and isna(key) and not self.hasnans:


KeyError: 'A2'

我该如何解决? 我想要的新 excel 表是:

姓名 ID 考试编号 理论考试时间 地址 技能测试时间 技能测试地址

您最好的选择可能是自己使用 openpyxl 读取 excel 文件。 您必须遍历您的行(每次跳 14 行)并获取BJ列中的值。 然后你从你的字典列表中创建一个数据框:

from openpyxl import load_workbook
import pandas as pd

wb = load_workbook(filename = 'Copy of pdf-v1.xlsx.xlsx')
data_sheet = wb['Sheet1']     # enter the name of the Sheet
candidates = []
for i in range(1, data_sheet.max_row, 14):
    candidate1 = dict()
    candidate1['name'] = data_sheet[f'B{i}'].value
    candidate1['ID'] = data_sheet[f'B{i+1}'].value
    candidate1['Exam id'] = data_sheet[f'B{i+2}'].value
    candidate1['Theory Test Time'] = data_sheet[f'B{i+7}'].value
    candidate1['Address'] = data_sheet[f'B{i+6}'].value
    candidate1['Skill Test Time'] = data_sheet[f'B{i+9}'].value
    candidate1['Skill Test Address'] = data_sheet[f'B{i+8}'].value
    candidates.append(candidate1)

    candidate2 = dict()
    candidate2['name'] = data_sheet[f'J{i}'].value
    candidate2['ID'] = data_sheet[f'J{i+1}'].value
    candidate2['Exam id'] = data_sheet[f'J{i+2}'].value
    candidate2['Theory Test Time'] = data_sheet[f'J{i+7}'].value
    candidate2['Address'] = data_sheet[f'J{i+6}'].value
    candidate2['Skill Test Time'] = data_sheet[f'J{i+9}'].value
    candidate2['Skill Test Address'] = data_sheet[f'J{i+8}'].value
    candidates.append(candidate2)

table = pd.DataFrame(candidates)
print(table)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM