首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3多处理

Python3多处理
EN

Stack Overflow用户
提问于 2017-06-25 20:07:20
回答 1查看 321关注 0票数 0

我是个十足的初学者。我摸索我的代码通过类比的例子,所以道歉的任何误用术语。

我在python 3中编写了一小部分代码,其中:

  • 获取用户输入(计算机上的文件夹)
  • 在文件夹中搜索pdf文件
  • 将PDF的每一页转换为具有顺序编号的图像。按编号顺序遍历jpgs,使其黑白相间。OCR扫描文件并将文本输出到对象中,将文本内容保存到.txt文件中(通过pytesseract)。删除jpgs,留下.txt文件。大部分时间都花在转换为jpgs,并可能使他们黑白。

代码可以工作,尽管我确信它可以被改进。这需要一段时间,所以我想我应该尝试使用池进行多处理。

我的代码似乎创建了池。我还可以获得该函数,以打印文件夹中的文件列表,因此它似乎以某种形式将列表传递给它。

我无法使它工作,现在已多次黑代码与各种错误。我认为主要的问题是,我是无知的。

我的代码开始了:

用户输入块(请求用户目录中的文件夹,检查它是否是有效文件夹等等)。

OCR块作为一个函数(解析PDF,然后将内容输出到单个.txt文件中)

对于循环块作为一个函数(应该循环遍历文件夹中的每个PDF并在其上执行OCR块。

多处理块(应该将目录中的文件列表提供给循环块)。

为了避免编写“战争与和平”,我在下面列出了循环块和多处理块的最后版本:

代码语言:javascript
复制
    #import necessary modules


home_path = os.path.expanduser('~')

#ask for input with various checking mechanisms to make sure a useful pdfDir is obtained
    pdfDir = home_path + '/Documents/' + input('Please input the folder name where the PDFs are stored. The folder must be directly under the Documents folder. It cannot have a space in it. \n \n Name of folder:') 




def textExtractor():
    #convert pdf to jpeg with a tesseract friendly resolution

    with Img(filename=pdf_filename, resolution=300) as img: #some can be encrypted so use OCR instead of other libraries

    #various lines of code here

    compilation_temp.close()

def per_file_process (subject_files):

        for pdf in subject_files:

            #decode the whole file name as a string 
            pdf_filename = os.fsdecode(pdf)

            #check whether the string ends in .pdf

        if pdf_filename.endswith(".pdf"):

            #call the OCR function on it
            textExtractor()


        else:
            print ('nonsense')


if __name__ == '__main__':

    pool = Pool(2)

    pool.map(per_file_process, os.listdir(pdfDir))

请问有人愿意/能指出我的错误吗?

工作时守则的有关部分:

代码语言:javascript
复制
#import necessary

home_path = os.path.expanduser('~')

#block accepting input

    pdfDir = home_path + '/Documents/' + input('Please input the folder name where the PDFs are stored. The folder must be directly under the Documents folder. It cannot have a space in it. \n \n Name of folder:') 



def textExtractor():
    #convert pdf to jpeg with a tesseract friendly resolution

    with Img(filename=pdf_filename, resolution=300) as img: #need to think about using generic expanduser or other libraries to allow portability
    #various lines of code to OCR and output .txt file
    compilation_temp.close()


subject_files = os.listdir(pdfDir)
for pdf in subject_files:
         #decode the whole file name as a string you can see
         pdf_filename = os.fsdecode(pdf)
        #check whether the string ends in /pdf
        if pdf_filename.endswith(".pdf"):
            textExtractor()

        else:
            #print for debugging
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-25 20:40:09

Pool.mapos.listdir返回的每个名称重复调用worker函数。在per_file_process中,subject_files是单个文件名,for pdf in subject_files:枚举名称中的单个字符。此外,listdir只显示基本名称,没有子目录,所以您没有在正确的位置查找pdf。您可以使用glob按扩展名进行筛选,并向文件返回工作路径。

你的例子让人困惑..。textExtractor()不接受参数,所以它如何知道它正在处理哪个文件?我正在讨论这个问题,并假设它确实走上了文件处理的道路。如果是这样的话,您可以很容易地并行化,只需通过map输入pdf目录即可。假设处理时间会随pdf的不同而变化,我会将文件设置为1,以便早期完成的工作人员能够抓取额外的文件进行处理。

代码语言:javascript
复制
from glob import glob
import os
from multiprocessing import Pool

def textExtractor(pdf_filename):
    #convert pdf to jpeg with a tesseract friendly resolution
    with Img(filename=pdf_filename, resolution=300) as img: #some can be encrypted so use OCR instead of other libraries

        #...various lines of code here
    compilation_temp.close()

if __name__ == '__main__':
    #pdfDir is the folder inputted by user
    with Pool(2) as pool:
        # assuming call signature: textExtractor(path_to_file)
        pool.map(textExtractor, 
            (filename for filename in glob(os.path.join(pdfDir, '*.pdf'))
            if os.path.isfile(filename))
            chunksize=1)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44750141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档