简体   繁体   中英

python pass excel file into function via command line

The version of Python I'm using is 2.7.

I have this function in a file called RunAPIGeocoder.py

APIGeocoder.geocode(excel_path=r'{}'.format(input_path))

I need to call this process in the command line and I want to simply pass through an excel file

if I change the function to

APIGeocoder.geocode(excel_path)

and run from CMD

python RunAPIGeocoder.py C:path\\Book12019_07_29_16_03_12.875947.xlsx

it will give me this error

NameError: name 'excel_path' is not defined

excel_path is a parameter in the geocode function

I should note I am not trying to use raw_input from the user because this process will be piped into another process and I do not want user input

part of the geocode function in APIGeocoder.py

def geocode(excel_path):
    try:
        print 'Geocoding in Process...Start Time: {}'.format(time.strftime('%c'))

        # Read in data
        df_input = pd.read_excel(excel_path, converters={'NodeId':str})

TL;DR:

Try changing APIGeocoder.geocode(excel_path) to APIGeocoder.geocode(excel_path=input_path) .

The first version you provided works because the variable that contains what you are passing from the console is input_path .

excel_path is the parameter of the geocode method. On the other hand, input_path is the argument you are passing to the method. In your first example, you were using input_path as an argument, but preprocessing it first:

APIGeocoder.geocode(excel_path=r'{}'.format(input_path))

In other words, you are telling the geocode method that its parameter excel_path shall contain the value r'{}'.format(input_path) , which is some processing that you do to the variable input_path . Read about the difference between parameters and arguments!

figured it out

def run_geocoder(f):
    APIGeocoder.geocode(excel_path=r'{}'.format(f))

run_geocoder(*sys.argv[1:])

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