Summary of 12 basic knowledge points of Python language
- 2020-04-02 13:51:11
- OfStack
Of 12 kinds of commonly used in python programming basics: regular expression substitution, directory traversal method, list to sort by column, heavy, dictionaries, dictionaries, lists, string transfers, object operation time, the command-line argument parsing (getopt), print formatted output, hexadecimal conversion, python call system command or script, read and write python files.
1. Regular expression substitution
Goal: replace overview.gif in the string line with another string
>>> line = '<IMG ALIGN="middle" SRC='#'" />'
>>> mo=re.compile(r'(?<=SRC=)"([w+.]+)"',re.I)
>>> mo.sub(r'"1****"',line)
'<IMG ALIGN="middle" SRC='#'" /span>
>>> mo.sub(r'replace_str_1',line)
'<IMG ALIGN="middle" replace_str_overview.gif BORDER="0" ALT="">'< /span>
>>> mo.sub(r'"testetstset"',line)
'<IMG ALIGN="middle" SRC='#'" /span>
Note: where \1 is the matched data and can be directly referenced in this way
2. Traverse the directory method
At some point, we need to traverse a directory to find a specific list of files, which can be easily traversed using the os.walk method
import os
fileList = []
rootdir = "/data"
for root, subFolders, files in os.walk(rootdir):
if '.svn' in subFolders: subFolders.remove('.svn') # Exclude specific directories
for file in files:
if file.find(".t2t") != -1:# Find a file with a particular extension
file_dir_path = os.path.join(root,file)
fileList.append(file_dir_path)
print fileList
3. List sort
If each element of the list is a tuple, we want to sort it according to a column of the tuple
In the following example, we sorted the tuple by the second and third columns of the tuple in reverse order (reverse=True).
>>> a = [('2011-03-17', '2.26', 6429600, '0.0'), ('2011-03-16', '2.26', 12036900, '-3.0'),
('2011-03-15', '2.33', 15615500,'-19.1')]
>>> print a[0][0]
2011-03-17
>>> b = sorted(a, key=lambda result: result[1],reverse=True)
>>> print b
[('2011-03-15', '2.33', 15615500, '-19.1'), ('2011-03-17', '2.26', 6429600, '0.0'),
('2011-03-16', '2.26', 12036900, '-3.0')]
>>> c = sorted(a, key=lambda result: result[2],reverse=True)
>>> print c
[('2011-03-15', '2.33', 15615500, '-19.1'), ('2011-03-16', '2.26', 12036900, '-3.0'),
('2011-03-17', '2.26', 6429600, '0.0')]
4. List deduplication (list uniq)
If you need to remove duplicate elements from a list, use the following method
>>> lst= [(1,'sss'),(2,'fsdf'),(1,'sss'),(3,'fd')]
>>> set(lst)
set([(2, 'fsdf'), (3, 'fd'), (1, 'sss')])
>>>
>>> lst = [1, 1, 3, 4, 4, 5, 6, 7, 6]
>>> set(lst)
set([1, 3, 4, 5, 6, 7])
5. Dict sort
Generally, we sort by the key of the dictionary, but if we want to sort by the value of the dictionary, we use the following method
>>> 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'), ('sss', '2'), ('ffff2', '3'), ('ffdf', '5')]
6, dictionaries, lists, strings to each other
The following is the generated database connection string, converted from the dictionary to the string
>>> 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 to a dictionary
>>> a = 'server=mpilgrim;uid=sa;database=master;pwd=secret'
>>> aa = {}
>>> for i in a.split(';'):aa[i.split('=',1)[0]] = i.split('=',1)[1]
...
>>> aa
{'pwd': 'secret', 'database': 'master', 'uid': 'sa', 'server': 'mpilgrim'}
7. Time object operation
Converts the time object to a string
>>> import datetime
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
'2011-01-20 14:05'
Time scale comparison
>>> import time
>>> t1 = time.strptime('2011-01-20 14:05',"%Y-%m-%d %H:%M")
>>> t2 = time.strptime('2011-01-20 16:05',"%Y-%m-%d %H:%M")
>>> t1 > t2
False
>>> t1 < t2
True
Time difference value calculation, calculate 8 hours ago time
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
'2011-01-20 15:02'
>>> (datetime.datetime.now() - datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M")
'2011-01-20 07:03'
Converts a string to a time object
>>> endtime=datetime.datetime.strptime('20100701',"%Y%m%d")
>>> type(endtime)
<type 'datetime.datetime'>
>>> print endtime
2010-07-01 00:00:00
Format the output from 1970-01-01 00:00:00 UTC to the current number of seconds
>>> import time
>>> a = 1302153828
>>> time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(a))
'2011-04-07 13:23:48'
8. Command line parameter analysis (getopt)
Usually when you write some daily operations scripts, you need to enter different command-line options to achieve different functions according to different conditions
The getopt module in Python provides a good implementation of command line parameter parsing, the following distance. See the following program:
#!/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:')
except 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 formats output
9.1 format the output string
Intercepts the string output. The following example will output only the first three letters of the string
>>> str="abcdefg"
>>> print "%.3s" % str
abc
Output as fixed width, insufficient to use space completion, the following example output width is 10
>>> str="abcdefg"
>>> print "%10s" % str
abcdefg
Intercepts a string and outputs it in a fixed width
>>> str="abcdefg"
>>> print "%10.3s" % str
abc
Floating-point type data bits reserved
>>> import fpformat
>>> a= 0.0030000000005
>>> b=fpformat.fix(a,6)
>>> print b
0.003000
Round round round round round round round round round round round round
>>> 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 conversion of base
Sometimes you need to do different base conversions, see the following example (%x hexadecimal,%d decimal,%o decimal)
>>> num = 10
>>> print "Hex = %x,Dec = %d,Oct = %o" %(num,num,num)
Hex = a,Dec = 10,Oct = 12
Python calls system commands or scripts
The system command is invoked with os.system(), and the output and return values cannot be obtained in the program
>>> import os
>>> os.system('ls -l /proc/cpuinfo')
>>> os.system("ls -l /proc/cpuinfo")
-r--r--r-- 1 root root 0 3 month 29 16:53 /proc/cpuinfo
0
The system command is called with os.popen(), and the output of the command can be obtained in the program, but the return value of the execution cannot be obtained
>>> out = os.popen("ls -l /proc/cpuinfo")
>>> print out.read()
-r--r--r-- 1 root root 0 3 month 29 16:59 /proc/cpuinfo
The system command is called with orders.getstatusoutput (), and the return value of the command output and execution is available in the program
>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
11. Python captures user Ctrl+C,Ctrl+D events
Sometimes, you need to capture the user's keyboard event in the program, such as CTRL +c exit, so you can exit the program more safely
try:
do_some_func()
except KeyboardInterrupt:
print "User Press Ctrl+C,Exit"
except EOFError:
print "User Press Ctrl+D,Exit"
12. Python reads and writes files
Read the file to the list at one time, the speed is fast, suitable for the file is relatively small
track_file = "track_stock.conf"
fd = open(track_file)
content_list = fd.readlines()
fd.close()
for line in content_list:
print line
Line by line, slow, not enough memory to read the entire file (the file is too big)
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) : write STR to a file. Write () does not add a new line after STR
Fd.writelines(content) : write the entire content to a file, as is, without adding anything after each line