如何在python中使用tabula阅读PDF时删除“Nan”值?

bfnvny8b  于 2023-04-19  发布在  Python
关注(0)|答案(2)|浏览(134)

**我正在使用tabula-py在python中读取我的课程时间表PDF文件,返回值'data'有很多'nan'值,我似乎无法清理。有人能建议解决方案吗?我应该使用其他东西来代替tabula-py吗?**我已经附加了PDF图片的链接。为了隐私,我已经从PDF中编辑了一些信息。1

代码如下:

import tabula

class ClassTimetable:
def __init__(self, filename):
    self.filename = filename

def read_data(self):
    data = tabula.read_pdf(self.filename, pages='all')
    # data1 = tabula.convert_into(self.filename, output_format="csv", output_path='file.csv')
    print(data)

我的输出如下:

[                                     Course Course Regn.  ... Unnamed: 2     Room
0                                Code Title Credit  Type  ...   GCR Code      No.
1                                     Critical and   NaN  ...        NaN      NaN
2                             1 18PDM202L Creative     0  ...         A-  wubaing
3                                  Thinking Skills   NaN  ...   ISOLATED      NaN
4                                       Management   NaN  ...        NaN      NaN
5                       2 18PDH102T Principles for     2  ...         A-      NaN
6                                        Engineers   NaN  ...   COMBINED      NaN
7   Professional Lab3 18EEC206J Analog Electronics     4  ...          B   boc5om
8                                      Generation,   NaN  ...        NaN      NaN
9                     4 18EEC208T Transmission & 3   NaN  ...        NaN      NaN
10                                    Distribution   NaN  ...          C  4qjaetp
11                                       Numerical   NaN  ...        NaN      NaN
12               5 18MAB202T Methods for Engineers     4  ...          D  vvbxlqp
13              6 18EEC205J Electrical Machines II     4  ...          E  drcfega
14                             7 18BTB101T Biology     2  ...          F      NaN
15                                  Electrical and   NaN  ...        NaN      NaN
16                                     Electronics   NaN  ...        NaN      NaN
17                    8 18EEC207J Measurements and     4  ...          G   koed72
18                                 Instrumentation   NaN  ...        NaN      NaN
19              9 18EEC205J Electrical Machines II     4  ...     P7-P8-  drcfega
20                                             NaN   NaN  ...        NaN      NaN
21                 10 18EEC206J Analog Electronics     4  ...     P3-P4-   boc5om
22                                  Electrical and   NaN  ...        NaN      NaN
23                                     Electronics   NaN  ...        NaN      NaN
24                       11 18EEC207J Measurements     4  ...        NaN      NaN
25                                             and   NaN  ...   P19-P20-      NaN
26                                 Instrumentation   NaN  ...        NaN      NaN
27                                        Total 23   NaN  ...        NaN      NaN

[28 rows x 8 columns]]

还有,“......”是什么意思?

flmtquvp

flmtquvp1#

**我想明白了。**我意识到,问题是库没有正确阅读行之间的分隔,所以我设置了'lattice= True'。这解决了我大约50%的问题,并意识到程序需要更大的特异性。

下载Tabula for windows并找到整个表的坐标以及单独的列。将数据馈送到tabula-py中,在构建选项'area='和'columns='下。我意识到使用这两个属性可能有点大材小用,但在格式化为.csv后,我所有的数据都整齐地放置在单独的列中,没有'Nan'值。下面附上我的代码:

import tabula
    
    class ClassTimetable:
    def __init__(self, filename):
        self.filename = filename

    def read_data(self):
        data = tabula.read_pdf(self.filename, pages='all', area=[162.498,141.6,546.248,538.736],
                               columns=[140.55, 172.53, 217.161, 277.400, 300.454, 339.127, 384.492, 419.446,
                                        491.585, 542.157], lattice=True)

        data1 = tabula.convert_into(self.filename, output_format="csv",  area=[162.498,141.6,546.248,538.736],
                                    columns=[140.559, 172.538, 217.161, 277.400, 300.454, 339.127, 384.492, 419.446,
                                             491.585, 542.157], lattice=True, output_path='file2.csv')
        return data

输出如下:

[    Unnamed: 0 Course\rTitle  ...                               Slot      GCR Code
0          1.0     18PDM202L  ...  Mr. R. Prathap\rChandran (102275)  A-\rISOLATED
1          2.0     18PDH102T  ...     Mr. Nizamudeen\rAnvar (102293)  A-\rCOMBINED
2          3.0     18EEC206J  ...  Dr.T.M.Thamizh\rThentral (101436)             B
3          4.0     18EEC208T  ...          Dr.S.Vidyasagar\r(100597)             C
4          5.0     18MAB202T  ...            Dr. M. Suresh\r(101984)             D
5          6.0     18EEC205J  ...     Dr. K. M, Ravi\rEswar (102699)             E
6          7.0     18BTB101T  ...               Mr.T.Anand\r(100034)             F
7          8.0     18EEC207J  ...        Mr.S.Raghavendran\r(102704)             G
8          9.0     18EEC205J  ...     Dr. K. M, Ravi\rEswar (102699)        P7-P8-
9         10.0     18EEC206J  ...  Dr.T.M.Thamizh\rThentral (101436)        P3-P4-
10        11.0     18EEC207J  ...        Mr.S.Raghavendran\r(102704)      P19-P20-
11         NaN            23  ...                                NaN           NaN

还是不知道". . .“是什么意思

myzjeezk

myzjeezk2#

您需要像下面这样将pandas_options传递给tabula.read_pdf:
pandas_options={'keep_default_na': False, 'na_values':['nan']

相关问题