In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to deal with command line parameters in Python. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
1.sys module
The sys module in Python has the argv function. When the execution of the main.py is triggered through the terminal, this function returns a list of all command line arguments. The first element in the return list is main.py.
Consider the following main.py example
Import syslist_of_arguments = sys.argvprint (list_of_args [0]) print (list_of_args [1]) print (list_of_args [2]) print (list_of_args [3])
Trigger main.py:
Python main.py first_arg "[second_arg]"{\" arg\ ": 3}"
Return:
Test.pyfirst_ ar [second _ arg] {"arg": 3} 2. Sys module with large parameters
This is a simple and powerful way to provide parameters to Python code. Instead of providing multiple parameters, it provides a single "large" parameter. This large parameter is a dictionary, the key represents the parameter name, and the value represents their corresponding value.
Because the dictionary parameter is represented as a string when read in Python, it should be converted to a dictionary. This can be done by using the ast.literal_eval or json.loads function. Ast or json modules need to be imported accordingly.
Consider the following example of main.py:
Import sysimport astraw_arguments = sys.argv [1] print (raw_arguments) arguments = ast.literal_eval (raw_arguments) print (arguments ['name']) # Johnprint (arguments [' surname']) # Doeprint (arguments ['age']) # 22
Trigger mian.py:
Python main.py "{\" name\ ":\" John\ ",\" surname\ ":\" Doe\ ",\" age\ ": 22}"
Return:
{"name": "John", "surname": "Doe", "age": 22} JohnDoe223.argparse module
If you want to provide an appropriate command line interface for your application, then argparse is the module to use. This is a mature module that provides out-of-the-box parameter parsing, help messages, and automatically throws errors when parameters are misused. Python installs this module by default.
To take full advantage of the capabilities provided by argparse, it takes some time to master. As an example, consider the following example main.py:
Import argparseparser = argparse.ArgumentParser (description='Personal information') parser.add_argument ('- name', dest='name', type=str, help='Name of the candidate') parser.add_argument ('- surname', dest='surname', type=str, help='Surname of the candidate') parser.add_argument ('- age', dest='age', type=int, help='Age of the candidate') args = parser.parse_args () print (args.name) print (args.surname) print (args.age)
After initializing the object for ArgumentParses, we use the add_argument function to add all the parameters. This function takes a number of parameters, including the parameter name (for example-- name), the target variable, the expected data type, the help message to display, and so on.
Trigger main.py:
Python main.py-name John-surname Doe-age 22
Return
JohnDoe22 above is how to deal with command line parameters in Python shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.