今天的第一个问题在运行时提示Traceback (most recent call last): File "C:\Users\86183\PycharmProjects\0609\main.py", line 34, in <module> references = extract_references_from_folder(r"C:\Users\86183\PycharmProjects\0609\1979-2010") File "C:\Users\86183\PycharmProjects\0609\main.py", line 29, in extract_references_from_folder "references": extract_references(filepath) File "C:\Users\86183\PycharmProjects\0609\main.py", line 9, in extract_references pdf = PyPDF2.PdfFileReader(f) File "C:\Users\86183\PycharmProjects\0609\venv\lib\site-packages\PyPDF2\_reader.py", line 1974, in __init__ deprecation_with_replacement("PdfFileReader", "PdfReader", "3.0.0") File "C:\Users\86183\PycharmProjects\0609\venv\lib\site-packages\PyPDF2\_utils.py", line 369, in deprecation_with_replacement deprecation(DEPR_MSG_HAPPENED.format(old_name, removed_in, new_name)) File "C:\Users\86183\PycharmProjects\0609\venv\lib\site-packages\PyPDF2\_utils.py", line 351, in deprecation raise DeprecationError(msg) PyPDF2.errors.DeprecationError: PdfFileReader is deprecated and was removed in PyPDF2 3.0.0. Use PdfReader instead. 要怎么处理
时间: 2023-07-18 11:40:29 浏览: 200
iOS版微信抢红包Tweak.zip小程序
这个错误是因为 PyPDF2 库在 3.0.0 版本中移除了 `PdfFileReader`,使用了 `PdfReader` 代替。因此,你需要将你的代码中所有使用 `PdfFileReader` 的地方改为使用 `PdfReader`。具体来说,你可以按照以下步骤进行修改:
1. 首先在你的代码中找到所有使用 `PdfFileReader` 的地方。
2. 将 `PdfFileReader` 改为 `PdfReader`。
3. 重新运行代码,检查是否还存在其他错误。
以下是修改后的代码:
```
import os
import PyPDF2
def extract_references(filepath):
references = []
with open(filepath, 'rb') as f:
pdf = PyPDF2.PdfReader(f) # 将 PdfFileReader 改为 PdfReader
for page in pdf.pages:
text = page.extract_text()
if text:
lines = text.split('\n')
for line in lines:
if line.startswith('[') and ']' in line:
references.append(line)
return references
def extract_references_from_folder(folderpath):
references = []
for filename in os.listdir(folderpath):
if filename.endswith('.pdf'):
filepath = os.path.join(folderpath, filename)
references.append({
"filename": filename,
"references": extract_references(filepath)
})
return references
references = extract_references_from_folder(r"C:\Users\86183\PycharmProjects\0609\1979-2010")
print(references)
```
注意,在使用 `PdfReader` 时,需要将打开文件的模式从 `'r'` 改为 `'rb'`,即使用二进制模式打开文件,以避免出现 `TypeError: 'str' does not support the buffer interface` 错误。
阅读全文