2016-09-14 106 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