首页 > 解决方案 > 在将数据从 pdfplumber 导入到 .csv 文件时需要帮助

问题描述

我使用 pdfplumber 从 pdf 中提取文本,但是当我尝试使用 to_csv 导入数据时抛出 #me 错误。在将数据导入 .csv 时需要帮助

import pdfplumber
import pandas as pd
import numpy as np
import os
import re
from collections import OrderedDict

pdf = pdfplumber.open('C:/Users/Desktop/Mydata.pdf')
page = pdf.pages[1-76]
text = page.extract_text()
text
print(text)



text2 = pd.DataFrame([text])

text2.to_csv("C:\\Users\\Desktop\\MyPDFData\\converted_text.csv")

没有得到导入文件中的数据只是得到了一个空文件

标签: pythonpdftext-extractiontabulapython-pdfreader

解决方案


你可能不需要熊猫来做这件事。只需先打开 CSV 引擎:

with open(your_csv_file_name, mode='w', newline='') as export_csv:
        csv_writer = csv.writer(export_csv, escapechar=' ', quoting=csv.QUOTE_NONE)
        csv_writer.writerow(text)

有一个很好的页面来了解 CSV 导出:

https://realpython.com/python-csv/?fireglass_rsn=true


推荐阅读