我正在为一个项目编译一些数据,我一直在使用PyPDF4从它的源PDF文件中读取这些数据,但我遇到了某些字符显示不正确的问题。这是我的密码:
from PyPDF4 import PdfFileReader
import pandas as pd
import numpy as np
import os
import xml.etree.cElementTree as ET
# File name
pdf_path = "PV-9-2020-10-23-RCV_FR.pdf"
# Results storage
results = {}
# Start page
page = 5
# Lambda to assign votes
serify = lambda voters, vote: pd.Series({voter.strip(): vote for voter in voters})
with open(pdf_path, 'rb') as f:
# Get PDF reader for PDF file f
pdf = PdfFileReader(f)
while page < pdf.numPages:
# Get text of page in PDF
text = pdf.getPage(page).extractText()
proposal = text.split("\n+\n")[0].split("\n")[3]
# Collect all pages relevant pages
while text.find("\n0\n") is -1:
page += 1
text += "\n".join(pdf.getPage(page).extractText().split("\n")[3:])
# Remove corrections
text, corrections = text.split("CORRECCIONES")
# Grab relevant text !!! This is where the missing characters show up.
text = "\n, ".join([n[:n.rindex("\n")] for n in text.split("\n:")])
for_list = "".join(text[text.index("\n+\n")+3:text.index("\n-\n")].split("\n")[:-1]).split(", ")
nay_list = "".join(text[text.index("\n-\n")+3:text.index("\n0\n")].split("\n")[:-1]).split(", ")
abs_list = "".join(text[text.index("\n0\n")+3:].split("\n")[:-1]).split(", ")
# Store data in results
results.update({proposal: dict(pd.concat([serify(for_list, 1), serify(nay_list, -1), serify(abs_list, 0)]).items())})
page += 1
print(page)
results = pd.DataFrame(results)
我遇到困难的字符不会出现在使用extractText
提取的文本中Ždanoka
例如变成"danoka
,Štefanec
变成-tefanc
。看起来大部分的角色都是东欧人,这让我觉得我需要一个拉丁语解码器
我已经浏览了PyPDF4的一些功能,它似乎有很多相关的编解码器,包括latin1
。我尝试使用PyPDF4.generic.codecs
模块中的不同函数对文件进行解码,要么字符不显示,要么代码在无法识别的字节处抛出错误
我还没有尝试在同一个文件的不同字节上使用多个编解码器,这似乎需要一些时间。我的代码中是否缺少可以轻松修复此问题的内容?或者我是否更可能将使用PyPDF4的函数来定制适合的解决方案
目前没有回答
相关问题 更多 >
编程相关推荐