This article describes 12 basic knowledge points of the Python language, including regular expression replacement, directory traversal, column sorting, deduplication, and dictionary sorting, for more information, see 12 basic knowledge commonly used in python Programming: regular expression replacement, directory traversal, column sorting, deduplication, and dictionary sorting, dictionary, list, string conversion, time object operation, command line parameter parsing (getopt), print formatted output, hexadecimal conversion, Python calls system commands or scripts, and Python reads and writes files.
1. Regular expression replacement
Purpose: to replace overview.gif in the string line with other strings.
The code is as follows:
>>> Line =''
>>> Mo = re. compile (r '(? <= SRC =) "([\ w + \.] +)" ', re. I)
>>> Mo. sub (r' "\ 1 *****" ', line)
'
>>> Mo. sub (r'replace _ str _ \ 1', line)
''</Span>
>>> Mo. sub (r' "testetstset" ', line)
'
Note: \ 1 is the matched data, which can be directly referenced in this way.
2. directory traversal
In some cases, we need to traverse a directory to find a specific file list, which can be traversed through the OS. walk Method, which is very convenient.
The code is as follows:
Import OS
FileList = []
Rootdir = "/data"
For root, subFolders, files in OS. walk (rootdir ):
If '. svn' in subFolders: subFolders. remove ('. svn') # exclude a specific directory
For file in files:
If file. find (". t2t ")! =-1: # search for files with a specific extension
File_dir_path = OS. path. join (root, file)
FileList. append (file_dir_path)
Print fileList
3. sort the list by column (list sort)
If each element in the list is a tuple, we want to sort it according to a column of the tuples. refer to the following method.
In the following example, we sort the data in the 2nd and 3rd columns of the tuples in reverse order (reverse = True)
The code is as follows:
>>> A = [('2017-03-17 ', '2. 26 ', 6429600, '0. 0'), ('2017-03-16 ', '2. 26', 12036900, '-3.0 '),
('2014-03-15 ', '2. 33', 2011,'-15615500 ')]
>>> Print a [0] [0]
2011-03-17
>>> B = sorted (a, key = lambda result: result [1], reverse = True)
>>> Print B
[('2014-03-15 ', '2. 33 ', 15615500,'-19.1 '), ('2017-03-17', '2. 26 ', 6429600, '0. 0 '),
('1970-03-16 ', '2. 26', 2011,'-12036900 ')]
>>> C = sorted (a, key = lambda result: result [2], reverse = True)
>>> Print c
[('2014-03-15 ', '2. 33 ', 15615500,'-19.1 '), ('2017-03-16', '2. 26', 12036900, '-3.0 '),
('1970-03-17 ', '2. 26', 2011, '0. 0')]
4. list deduplication (list uniq)
To delete repeated elements in the list, use the following method:
The code is as follows:
>>> Lst = [(1, 'SS'), (2, 'fsdf '), (1, 'sss'), (3, 'fd')]
>>> Set (lst)
Set ([(2, 'fsdf '), (3, 'fd'), (1, 'SS')])
>>>
>>> Lst = [1, 1, 3, 4, 4, 5, 6, 7, 6]
>>> Set (lst)
Set ([1, 3, 4, 5, 6, 7])
5. dictionary sorting (dict sort)
Generally, we sort the keys of the dictionary. However, if we want to sort the keys of the dictionary, we use the following method:
The code is as follows:
>>> From operator import itemgetter
>>> Aa = {"a": "1", "sss": "2", "ffdf": '5', "ffff2": '3 '}
>>> Sort_aa = sorted (aa. items (), key = itemgetter (1 ))
>>> Sort_aa
[('A', '1'), ('SS', '2'), ('ffff2', '3'), ('ffdf ', '5')]
6. dictionary, list, and string conversion
The following describes how to generate a database connection string and convert it from dictionary to string.
The code is as follows:
>>> Params = {"server": "mpilgrim", "database": "master", "uid": "sa", "pwd": "secret "}
>>> ["% S = % s" % (k, v) for k, v in params. items ()]
['Server = mpilgrim', 'uid = sa ', 'database = master', 'pwd = secret']
>>> ";". Join (["% s = % s" % (k, v) for k, v in params. items ()])
'Server = mpilgrim; uid = sa; database = master; pwd = secret'
The following example converts a string into a dictionary.
The code is as follows:
>>> A = 'server = mpilgrim; uid = sa; database = master; pwd = secret'
>>> Aa = {}
>>> For I in. split (';'): aa [I. split ('=', 1) [0] = I. split ('=', 1) [1]
...
>>> Aa
{'Pwd': 'Secret', 'database': 'master', 'uid': 'sa ', 'server': 'mpilgrim '}
7. time object operations
Converts a time object to a string.
The code is as follows:
>>> Import datetime
>>> Datetime. datetime. now (). strftime ("% Y-% m-% d % H: % M ")
'2017-01-20'
Time size comparison
The code is as follows:
>>> Import time
>>> T1 = time. strptime ('2017-01-20 ', "% Y-% m-% d % H: % M ")
>>> T2 = time. strptime ('2017-01-20 ', "% Y-% m-% d % H: % M ")
>>> T1> t2
False
>>> T1 <t2
True
Calculate the time difference, calculated 8 hours ago
The code is as follows:
>>> Datetime. datetime. now (). strftime ("% Y-% m-% d % H: % M ")
'2017-01-20'
>>> (Datetime. datetime. now ()-datetime. timedelta (hours = 8). strftime ("% Y-% m-% d % H: % M ")
'2017-01-20'
Converts a string to a time object.
The code is as follows:
>>> Endtime = datetime. datetime. strptime ('20140901', "% Y % m % d ")
>>> Type (endtime)
>>> Print endtime
2010-07-01 00:00:00
Format the output in seconds from 00:00:00 UTC to the current time
The code is as follows:
>>> Import time
>>> A = 1302153828
>>> Time. strftime ("% Y-% m-% d % H: % M: % S", time. localtime ())
'2017-04-07 13:23:48'
8. command line parameter parsing (getopt)
Generally, when writing Daily O & M scripts, you must enter different command line options based on different conditions to implement different functions.
In Python, The getopt module is provided to parse the command line parameters. The distance is described below. See the following program:
The code is as follows:
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
Import sys, OS, getopt
Def usage ():
Print '''''
Usage: analyse_stock.py [options...]
Options:
-E: Exchange Name
-C: User-Defined Category Name
-F: Read stock info from file and save to db
-D: delete from db by stock code
-N: stock name
-S: stock code
-H: this help info
Test. py-s haha-n "HA Ha"
'''
Try:
Opts, args = getopt. getopt (sys. argv [1:], 'He: c: f: d: n: s :')
Counter T getopt. GetoptError:
Usage ()
Sys. exit ()
If len (opts) = 0:
Usage ()
Sys. exit ()
For opt, arg in opts:
If opt in ('-H',' -- help '):
Usage ()
Sys. exit ()
Elif opt = '-D ':
Print "del stock % s" % arg
Elif opt = '-f ':
Print "read file % s" % arg
Elif opt = '-C ':
Print "user-defined % s" % arg
Elif opt = '-E ':
Print "Exchange Name % s" % arg
Elif opt = '-s ':
Print "Stock code % s" % arg
Elif opt = '-N ':
Print "Stock name % s" % arg
Sys. exit ()
9. print formatted output
9.1 format the output string
Truncates string output. in the following example, only the first three letters of the string are output.
The code is as follows:
>>> Str = "abcdefg"
>>> Print "%. 3 s" % str
Abc
Output by fixed width. if space is not used to complete the output, the output width in the following example is 10.
The code is as follows:
>>> Str = "abcdefg"
>>> Print "% 10 s" % str
Abcdefg
Truncate the string and output the string according to the fixed width.
The code is as follows:
>>> Str = "abcdefg"
>>> Print "% 10.3 s" % str
Abc
Reserved floating point data bits
The code is as follows:
>>> Import fpformat
>>> A = 0.0030000000005
>>> B = fpformat. fix (a, 6)
>>> Print B
0.003000
Round the floating point number, mainly using the round function
The code is as follows:
>>> From decimal import *
>>> A = "2.26"
>>> B = "2.29"
>>> C = Decimal (a)-Decimal (B)
>>> Print c
-0.03
>>> C/Decimal (a) * 100
Decimal ('-1.327433628318584070796460177 ')
>>> Decimal (str (round (c/Decimal (a) * 100, 2 )))
Decimal ('-1.33 ')
9.2 hexadecimal conversion
In some cases, different hexadecimal conversions are required. refer to the following example (% x hexadecimal, % d decimal, % o decimal)
The code is as follows:
>>> Num = 10
>>> Print "Hex = % x, Dec = % d, Oct = % o" % (num, num, num)
Hex = a, Dec = 10, Oct = 12
10. Python calls system commands or scripts
Using OS. system () to call system commands, the output and return values cannot be obtained in the program.
The code is as follows:
>>> Import OS
>>> OS. system ('ls-l/proc/cpuinfo ')
>>> OS. system ("ls-l/proc/cpuinfo ")
-R -- 1 root 0 March 29 16:53/proc/cpuinfo
0
Use OS. popen () to call the system command. the command output can be obtained in the program, but the returned value cannot be obtained.
The code is as follows:
>>> Out = OS. popen ("ls-l/proc/cpuinfo ")
>>> Print out. read ()
-R -- 1 root 0 March 29 16:59/proc/cpuinfo
Use commands. getstatusoutput () to call system commands. The program can obtain the return values of command output and execution.
The code is as follows:
>>> Import commands
>>> Commands. getstatusoutput ('ls/bin/LS ')
(0, '/bin/LS ')
11. Python captures user Ctrl + C, Ctrl + D events
Sometimes, you need to capture user keyboard events in the program, such as ctrl + c exit, so as to better secure exit the program
The code is as follows:
Try:
Do_some_func ()
Except t KeyboardInterrupt:
Print "User Press Ctrl + C, Exit"
Failed T EOFError:
Print "User Press Ctrl + D, Exit"
12. reading and writing Python files
Reads files to the list at a time, which is fast and applicable when the file is small.
The code is as follows:
Track_file = "track_stock.conf"
Fd = open (track_file)
Content_list = fd. readlines ()
Fd. close ()
For line in content_list:
Print line
Row-by-row reading, slow speed, applicable to not enough memory to read the entire file (the file is too large)
The code is as follows:
Fd = open (file_path)
Fd. seek (0)
Title = fd. readline ()
Keyword = fd. readline ()
Uuid = fd. readline ()
Fd. close ()
Differences between write and writelines
Fd. write (str): writes str to a file. write () does not add a linefeed after str.
Fd. writelines (content): writes all content to a file. it is written as is without adding anything to the end of each line.