[英]python pass excel file into function via command line
The version of Python I'm using is 2.7. 我使用的Python版本是2.7。
I have this function in a file called RunAPIGeocoder.py 我在名为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 我需要在命令行中调用此过程,而我只想通过一个Excel文件
if I change the function to 如果我将功能更改为
APIGeocoder.geocode(excel_path)
and run from CMD 并从CMD运行
python RunAPIGeocoder.py C:path\\Book12019_07_29_16_03_12.875947.xlsx 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 excel_path是地址解析功能中的参数
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 我应该注意,我不尝试使用用户的raw_input,因为此进程将通过管道传递到另一个进程中,并且我不希望用户输入
part of the geocode function in APIGeocoder.py 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: TL; DR:
Try changing APIGeocoder.geocode(excel_path)
to APIGeocoder.geocode(excel_path=input_path)
. 尝试将APIGeocoder.geocode(excel_path)
更改为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 . 您提供的第一个版本有效,因为包含从控制台传递的内容的变量是input_path 。
excel_path is the parameter of the geocode method. excel_path是地址解析方法的参数。 On the other hand, input_path is the argument you are passing to the method. 另一方面, input_path是您要传递给方法的参数。 In your first example, you were using input_path as an argument, but preprocessing it first: 在第一个示例中,您使用input_path作为参数,但首先对其进行了预处理:
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 . 换句话说,您要告诉地址解析方法其参数excel_path应该包含值r'{}'。format(input_path) ,这是您对变量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:])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.