2016-09-14 107 views
-3

我想知道山楂我可以通过这些文件作为一个PY文件的参数,并从这些文件传递参数的Python脚本

pd.read_csv('C:/Users/Demonstrator/Downloads/file1.csv',delimiter=';', parse_dates=[0], infer_datetime_format = True) 
df_energy2=pd.read_csv('C:/Users/Demonstrator/Downloads/file2.csv', delimiter=';', parse_dates=[0], infer_datetime_format = True) 

构建数据帧谢谢

+0

嗯?你想*接受*文件名作为命令行参数吗?询问如何*传递它们(即如何将它们指定为命令行参数)需要相当多的上下文(你是否从另一个Python脚本调用一个Python脚本?如果是,怎么做?) –

+0

...如果你的意思是请询问如何*接受*命令行参数,请参阅['sys.argv'](https://docs.python.org/2/library/sys.html)或[argparse模块](https:// docs.python.org/3/library/argparse.html)。 –

回答

1

传递参数很简单。你可以看看https://docs.python.org/3/library/argparse.html

最简单的方式来传递参数给一个Python脚本是通过将这些行你python脚本和修改它们按需要:

if __name__ == '__main__': 
    import sys 
    if len(sys.argv) != 2 # here I am expecting only one commandline agrument 
     print("USAGE: <Scriptname> <commandlineargument>") 

     sys.exit(1) 
    commandlineValue = sys.argv[1] # sys.argv[0] contains the file name you are running 
    # Do what ever you want to do with the commandlineValue, it will just print it 
    print ("CommandlineValue Passed is : {}".format(commandlineValue)) 
+0

谢谢,它的作品:) – Poisson

+0

欢迎您@CyrineEzzahra – LearningNinja