简体   繁体   English

传递参数python脚本

[英]passing arguments python script

I am trying to know haw can I pass these files as arguments in a py file, and construct dataframe from these files我想知道我可以将这些文件作为 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)

Thank you谢谢

Passing arguments is simple.传递参数很简单。 You can have a look at https://docs.python.org/3/library/argparse.html你可以看看https://docs.python.org/3/library/argparse.html

The most easiest way to pass argument to a python script is by adding these line to you python script and modifying them as per need:将参数传递给 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM