简体   繁体   中英

How to acess the return values from one function to other function using python3?

How to acess the return values from one function to other function using python3? I cannot able to pass the return function from one function to other? I am new to programming please help me

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)

Just return from function a like you did for tesseract function, also you don't need input_file as an argument to a , since you get that from tesseract function

def a():
    #Pass filename to tesseract
    input_file = tesseract(filename)

    #Use returned value from tesseract here
    for i in input_file: 
        print(i)

A simpler example is below, where func1 returns to func2 and func2 returns to 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))

The output will be

3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM