12 Essential Python Programming Basics: Regular Expressions, Directory Traversal, List Sorting, Deduplication, Dictionary Sorting, Data Conversion, Time Operations, Command‑Line Parsing, Print Formatting, Base Conversion, System Commands, Signal Handling, and File I/O
This article presents a concise collection of twelve fundamental Python techniques—including regex substitution, directory walking, list sorting and deduplication, dictionary sorting, conversions between dict/list/string, time manipulation, getopt argument parsing, formatted printing, numeral base conversion, executing system commands, handling Ctrl+C/Ctrl+D signals, and file reading/writing—each illustrated with ready‑to‑run code snippets.
1. Regular Expression Replacement
>> line = '<IMG ALIGN="middle" SRC=\'#\'" /span>' >> 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="">' >> mo.sub(r'"testetstset"', line) '<IMG ALIGN="middle" SRC=\'#\'" /span>'
Note: \1 refers to the captured group.
2. Directory Traversal
import os fileList = [] rootdir = "/data" for root, subFolders, files in os.walk(rootdir): if '.svn' in subFolders: subFolders.remove('.svn') # exclude specific dirs for file in files: if file.find('.t2t') != -1: # find files with .t2t extension file_dir_path = os.path.join(root, file) fileList.append(file_dir_path) print fileList
3. List Sorting by Column
>> 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')] >> 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
>> 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. Dictionary Sorting by Value
>> 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. Conversions Between Dictionary, List, and 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' >> 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 Operations
>> import datetime >> datetime.datetime.now().strftime("%Y-%m-%d %H:%M") '2011-01-20 14:05' >> 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 >> a = 1302153828 >> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(a)) '2011-04-07 13:23:48'
8. Command‑Line Argument Parsing with getopt
#!/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
9. Print Formatting
9.1 Formatting strings
str = "abcdefg" print "%.3s" % str abc print "%10s" % str abcdefg print "%10.3s" % str abc
9.2 Base conversion
>> num = 10 >> print "Hex = %x, Dec = %d, Oct = %o" % (num, num, num) Hex = a, Dec = 10, Oct = 12
10. Executing System Commands from Python
>> import os >> os.system('ls -l /proc/cpuinfo') 0 >> out = os.popen('ls -l /proc/cpuinfo') >> print out.read() >> import commands >> commands.getstatusoutput('ls /bin/ls') (0, '/bin/ls')
11. Capturing Ctrl+C and Ctrl+D Signals
try: do_some_func() except KeyboardInterrupt: print "User Press Ctrl+C, Exit" except EOFError: print "User Press Ctrl+D, Exit"
12. Reading and Writing Files
# Fast way – read whole file into a list 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 – suitable for large files fd = open(file_path) fd.seek(0) title = fd.readline() keyword = fd.readline() uuid = fd.readline() fd.close() # Writing – write vs writelines fd.write(str) # no newline added fd.writelines(content) # writes the iterable as‑is
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
