如何使用python3处理从一个函数到另一个函数的返回值?

时间:2019-05-23 06:44:36

标签: python-3.x function

如何使用python3处理从一个函数到另一个函数的返回值?我不能将return函数从一个函数传递给另一个函数吗? 我是编程新手,请帮助我

import os
import sys
import pytesseract
from PIL import Image 
from pdf2image import convert_from_path 


filename = "C:\\Users\\subashini.u\\Desktop\\tesseract-python\\penang_40.2.pdf"

def tesseract(filename): 
    PDF_file = filename 
    pages = convert_from_path(PDF_file, 500)  
    image_counter = 1

    for page in pages:  
        filename = "page_"+str(image_counter)+".jpg"
        page.save(filename, 'JPEG') 
        image_counter = image_counter + 1

    filelimit = image_counter-1
    outfile = "C:\\Users\\subashini.u\\Desktop\\tesseract-python\\text_file.txt"
    f = open(outfile, "a",encoding = "utf-8") 

    for i in range(1, filelimit + 1): 
        filename = "page_"+str(i)+".jpg"
        text = str(((pytesseract.image_to_string(Image.open(filename))))) 
        text = text.replace('-\n', '')     
        #print(text)
        f.write(text) 

    f.close() 
    f1 = open(outfile, "r",encoding = "utf-8") 
    input_file = f1.readlines()
    return input_file


def a(input_file): 
    for i in input_file: # i want to acess the return value here 
        print(i)

a(input_file)

1 个答案:

答案 0 :(得分:0)

就像从a函数一样从函数tesseract返回,也不需要input_file作为a的参数,因为您可以从{ {1}}功能

tesseract

下面是一个更简单的示例,其中def a(): #Pass filename to tesseract input_file = tesseract(filename) #Use returned value from tesseract here for i in input_file: print(i) 返回到func1,而func2返回到func2

main

输出将为

def func1(a):

    #return value from func1
    return a

def func2(b):

    #Get return value from func1
    x = func1(b)

    #Return value from func2
    return x

print(func2(3))