Python ConfigParse module usage, pythonconfigparse
This article describes how to use the ConfigParse module in python and shares it with you for your reference. The specific method is as follows:
The ConfigParse. RawConfigParse class is generally used for write configuration.
Use ConfigParse. ConfigParse class for read Configuration
The Code is as follows:
Write the configuration file:
Import ConfigParser import time config = ConfigParser. rawConfigParser () task ={} task ["id"] = 1 task ["package"] = "exe" task ["timeout"] = 150 task ["dst_filename"] = "1.exe" task ["custom"] = "" config. add_section ("analysis") # Add section config. set ("analysis", "id", task ["id"]) # Add option config. set ("analysis", "target", task ["dst_filename"]) config. set ("analysis", "package", task ["package"]) config. set ("analysis", "timeout", task ["timeout"]) config. set ("analysis", "started", time. asctime () fp = open ("analy. conf "," w ") config. write (fp) # writing to a file
The running result is as follows:
[analysis]started = Tue Apr 10 15:40:51 2012package = exeid = 1timeout = 150target = 1.exe
Read the configuration file:
import ConfigParser config = ConfigParser.ConfigParser() config.read("analy.conf") if config.has_option("analysis", "timeout"): print config.get("analysis", "timeout") print config.sections() print config.get("analysis", "package") print config.getint("analysis", "id")
The output is as follows:
150['analysis']exe1
I hope this article will help you with Python programming.
Python: the usage of configparse and optparse is different. An example is provided.
Configparse: used to interpret the config configuration file
Optparse: Application and Interpretation of the parameters set in the command line when calling a program
How to Use parse in the nltk package of python to analyze sentences and code
See:
Nltk.googlecode.com/..graph0
Www.ibm.com/..pnltk/