Python module knowledge 8: configparser, compression module, pythonconfigparser

Source: Internet
Author: User

Python module knowledge 8: configparser, compression module, pythonconfigparser


I. configparser Module

Configparser is used to process files in a specific format. In essence, it uses open to operate files.

The file format is as follows:

1. Read the configuration file.

-Read (filename) directly reads the INI File Content

-Sections () obtains all sections and returns them as a list.

-Options (section): obtain all options of this section.

-Items (section) obtains all key-value pairs of this section.

-Get (section, option): get the option value in section. The return value is of the string type.

-Getint (section, option) gets the option value in the section, returns the int type, and the corresponding getboolean () and getfloat () functions.

2. Basic write configuration file

-Add_section (section): Add a new section.

-Set (section, option, value) to set the option in section. You must call write to write the content to the configuration file.


Case 1: values under nodes, key-value pairs, keys, and keys

Import configparser # import module
Config = configparser. ConfigParser () # required loading statement
Config.read('black.txt ', encoding = 'utf-8') # Read content from the file
Ret = config. sections () # retrieve all nodes
Ret2 = config. items ("sec1") # retrieve all key-value pairs under Node value 1
Ret3 = config. options ('sec1') # obtain all keys under Node value 1
Ret4 = config. get ('sec1', "k1") # obtain the value of Node 1 and key 1
Print (ret) # execution result ['sec1', 'sec2', 'sec3']
Print (ret2) # execution result [('k1 ', 'v1 # value'), ('k2', 'v2 # value')]
Print (ret3) # execution result ['k1', 'k2']
Print (ret4) # execution result v1 # Value

Execution result:


Case 2: Operation nodes

# Adding nodes and key-value pairs

Import configparser # import module
Config = configparser. ConfigParser () # required loading statement
Config. add_section ("sec4") # Add a node
Config. set ("sec4", "k2", "jjj") # Add a key-Value Pair under a node
Config.write(open('black.txt ', 'A '))

Execution result:

Case 3: Check nodes

# Check whether a node exists

Import configparser # import module
Config = configparser. ConfigParser () # required loading statement
Config.read('black.txt ', encoding = 'utf-8') # Read content from the file
# Check whether a node exists
Has_sec = config. has_section ("sec2") # Check whether the node "sec2" exists"
Print (has_sec)
Execution result: TRUE

Case 4: delete a node

# Deleting key-value pairs under a node or node

Import configparser # import module
Config = configparser. ConfigParser () # required loading statement
Config.read('black.txt ', encoding = 'utf-8') # Read content from the file
Config. remove_section ('sec3') # delete a node
# Config. remove_option ('sec3', 'k1 ') # delete a node
Config.write(open('black.txt ', 'w '))

Ii. zipfile compression module

1) Compression

Import zipfile

Zw.zipfile.zipfile('a.zip ', 'w ')

Z. write ('hh. xml ')

Z. write ('hhnew3 ')

Z. close ()


2) Extract

Import zipfile

Zw.zipfile.zipfile('a.zip ', 'R ')

Z. extractall () # decompress all

For item in z. namelist ():

Print (item, type (item ))

Z. close ()


3. decompress the tar folder

1) Compression

Import tarfile

Tar = tarfile.open('your.tar ', 'w') # compress the tar file

Tar. add ('hh. xml', arcname = 'hh1. xml') # Change the name of hh1 when hh is compressed.

Tar. add ('hhnew3', arcname = 'hh3 ')

Tar. close ()


2) decompress

Tar = tarfile.open('your.tar ', 'R ')

# Tar. extractall () # All decompression problems can be set.

# Print all objects

For item in tar. getmembers ():

Print (item, type (item ))

# Single decompressed File

Obj = tar. getmember ("hh1.xml ")

Tar. extract (obj)

Tar. close ()


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.