python commonly used standard library

 

The glob module provides a function for matching files meet the requirements of:

Import glob 
List = glob.glob, and ( " * .py " )   # matches all matching file names in the current directory (including the extension), returns a list of strings 
Print (List)   # [ 'the test.py', ' test2.py ']

 

 

 

string matching the regular re module

Import Re 
List = the re.findall (R & lt " \ BH " , " Hi His Hello " )   # Get all meet the requirements match, to return a list of strings 
Print (List)   # [ 'H', 'H', ' H '] 

STR = the re.sub (R & lt " \ BH " , " H " , " Hi His Hello " )   # with a second string replaces the first regular expression match to all substrings. Immutable string, returns a copy of the. 
Print (str)   # Hi His-the Hello

 

 

 

datetime datetime module

from datetime Import DATE 
now = date.today ()
 Print (now)   # 2019-08-07, which is the default format 

Print (now.strftime ( " % Y.% m.% D " ))      # 2019.08.07 format date and time of the returned string corresponding to the date and time the object 

# Print (now.strftime ( "% m% on the Y month% d day")) # this code will be given, this is the year, month, day, hour module key words, you can not write directly to 

Print (now.strftime ( ' % the Y-% m {} {} {}% d ' ) .format ( " Year " , " month " , " day " ))   # August 2019 07 Japan, the returned string formatting 

birthdaydate = (2000,1,1)   # construct a date object parameters: year, month, day 
Print (Birthday)    # 2000-01-01 

Age = NOW- Birthday
 Print (Age)   # 7158 Days, 0:00:00 
Print (age.days)   # 7158

 

 

 

This may be used to format the output:

= str " I am {}, {} years old this year, " .format ( " Joe Smith " , 12 )
 Print (str) # I'm Joe Smith, 12 years old 

str = " I am {name}, {age} years old this year " .format (name = " Joe Smith " , Age = 12)    # specify the name to assign by name 
Print (str) # I'm Joe Smith, 12 years old

 

 

 

data compression

Support data packing, compression modules: zlib, gzip, bz2, zipfile, and tarfile.

 

Data compression:

Import zlib 
DATAl = bytes ( " love China Love China Love China Love China Love China " , " UTF-. 8 " )
 Print (len (DATAl))   # 60 

DATA2 = zlib.compress (DATAl)   # compressed data , if the parameter bytes. Parameters unchanged, a copy is returned 
Print (len (DATA2))   # 24 

DATA3 = zlib.decompress (DATA2)   # decompression parameters unchanged, returns a copy of 
Print (len (DATA3))   # 60 
Print (len (DATA2))   # 24

 

 

Compressed file:

Import zipfile 
F = zipfile.ZipFile ( ' 1.zip ' , ' W ' , zipfile.ZIP_DEFLATED)
 "" " 
call zipfile module ZipFile class constructor, creates a compressed file object 
three parameters: the name of the compressed file, read and write permissions, compression type 
compression have write permission, extracting permissions must read 
the type of compression two constant values: ZIP_STORE (default value does not compress data, the same size), ZIP_DEFLATED (data to be compressed, the file size becomes small) 
"" " 

f.write ( " 1.txt " )   # compressed file 1.txt 
f.write ( " 1.mp3 " )   # compressed files 1.mp3 
f.write ( ' 1.mp4" ' )   # compression File 1.MP4 
# That there are three files in 1.zip

f.close()

 


unzip files:

Import ZipFile 
F = zipfile.ZipFile ( ' 1.zip ' )   # create a compressed file object, the two parameters are the default values (+ does not read compressed data) 
f.extractall ( " . 1 " )   # Extract to folder. 1 
F .close ()


The modules have a plurality of methods, only briefly described herein below.



Guess you like

Origin www.cnblogs.com/chy18883701161/p/11300365.html