В python3 Tabula.read_pdf возвращает TypeError: ожидаемый объект str, bytes или os.PathLike, а не встроенный_функция_или_метод. Как мне заставить это работать? - PullRequest
0 голосов
/ 08 марта 2020

Я запускаю свой проект очистки в Блокнотах Jupyter на моем сервере, используя python3. По какой-то причине ошибки Tabula-py / Tabula при запуске Tabula.read_pdf и возвращении TypeError: ожидаемый объект str, bytes или os.PathLike, а не builtin_function_or_method. Как мне заставить это работать? Я передаю фактический файл PDF.

Мой код с ошибками

import tabula
df = tabula.read_pdf("20200125-sitrep-5-2019-ncov.pdf", pages=all)

Моя ошибка

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-4f86b7402956> in <module>
----> 1 df = tabula.read_pdf("20200125-sitrep-5-2019-ncov.pdf", pages=all)

/usr/local/lib/python3.7/dist-packages/tabula/io.py in read_pdf(input_path, output_format,       encoding, java_options, pandas_options, multiple_tables, user_agent, **kwargs)
320 
321     try:
--> 322         output = _run(java_options, kwargs, path, encoding)
323     finally:
324         if temporary:

/usr/local/lib/python3.7/dist-packages/tabula/io.py in _run(java_options, options, path, encoding)
 83             stderr=subprocess.PIPE,
 84             stdin=subprocess.DEVNULL,
---> 85             check=True,
 86         )
 87         if result.stderr:

/usr/lib/python3.7/subprocess.py in run(input, capture_output, timeout, check, *popenargs, **kwargs)
470         kwargs['stderr'] = PIPE
471 
--> 472     with Popen(*popenargs, **kwargs) as process:
473         try:
474             stdout, stderr = process.communicate(input, timeout=timeout)

/usr/lib/python3.7/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
773                                 c2pread, c2pwrite,
774                                 errread, errwrite,
--> 775                                 restore_signals, start_new_session)
776         except:
777             # Cleanup if the child failed starting.

/usr/lib/python3.7/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
1451                             errread, errwrite,
1452                             errpipe_read, errpipe_write,
-> 1453                             restore_signals, start_new_session, preexec_fn)
1454                     self._child_created = True
1455                 finally:

TypeError: expected str, bytes or os.PathLike object, not builtin_function_or_method

Мой PDF называется 20200125-sitrep- 5-2019-ncov.pdf. Это PDF, который я вычеркнул - https://www.who.int/docs/default-source/coronaviruse/situation-reports/20200125-sitrep-5-2019-ncov.pdf?sfvrsn=429b143d_8

enter image description here

1 Ответ

0 голосов
/ 08 марта 2020

Tabula не подходит для работы на сервере или в вертикальной среде, поэтому я решил использовать другую библиотеку под названием Camelot.

Установленный Camelot

pip install camelot-py

Импорт Camelot

import camelot

Мой новый код

tables = camelot.read_pdf('20200125-sitrep-5-2019-ncov.pdf', pages='3', process_background=True)
tables.export('20200125-sitrep-5-2019-ncov.csv', f='csv', compress=True) 
tables[0]
tables[0].parsing_report
{
    'accuracy': 99.02,
    'whitespace': 12.24,
    'order': 1,
    'page': 1
}
tables[0].to_csv('foo.csv') # to_json, to_excel, to_html
df_1 = tables[0].df # get a pandas DataFrame!

Документацию можно найти здесь - https://camelot-py.readthedocs.io/en/master/user/quickstart.html дальнейшее чтение https://camelot-py.readthedocs.io/en/master/user/advanced.html#advanced

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...