传递参数python脚本

时间:2016-09-14 16:20:02

标签: python pandas

我试图知道如何将这些文件作为py文件中的参数传递,并从这些文件构造数据框

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)

谢谢

1 个答案:

答案 0 :(得分: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))