gpt4 book ai didi

python - 在 python 中将函数应用于目录的每个元素的内容时出现问题?

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:04 24 4
gpt4 key购买 nike

我正在尝试从目录中获取多个 .pdf 文件的内容,以便使用 tika 库将它们转换为文本,但是我相信我没有正确读取 .pdf 文件对象。这是我到目前为止尝试过的:

输入:

for filename in sorted(glob.glob(os.path.join(input_directory, '*.pdf'))):
with open(filename,"rb") as f:
print(f)
text = parser.from_file(f)

输出:

<_io.BufferedReader name='/Users/user/Downloads/pdf-files/a_pdf_file.pdf'>
AttributeError: '_io.BufferedReader' object has no attribute 'decode'

在 python 中遍历文件内容的最有效方法是什么?

最佳答案

tika 解析器接收路径并打开文件本身:

for filename in sorted(glob.glob(os.path.join(input_directory, '*.pdf'))):
parsed = parser.from_file(filename)
text = parsed['content']

关于python - 在 python 中将函数应用于目录的每个元素的内容时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39925118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com