re — Regular expression operations
import re
m = re.search('(?<=abc)def', 'abcdef')
m.group(0)
#'def'
JSON encoder and decoder
https://docs.python.org/3/library/json.html
JSON | Python |
---|---|
object | dict |
array | list |
string | str |
number (int) | int |
number (real) | float |
urllib
#AU Crawler
import re
import urllib.request
import html2text
with urllib.request.urlopen('http://www.asia.edu.tw/news1.php') as response:
html = response.read().decode('utf-8')
pattern = '<font color="#446666" face="微軟正黑體" size="2">'
for pos in re.finditer(pattern, html):
pos2 = html.find('</font>', pos.end())
sub = html[pos.end():pos2]
print(sub)
CSV
https://docs.python.org/3/library/csv.html
#Write CSV
import csv
with open('names.csv', 'w', newline='') as csvfile:
fieldnames = ['first_name', 'last_name']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
#Read CSV
import csv
with open('names.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['first_name'], row['last_name'])
shutil-— High-level file operations
from shutil import copyfile
from shutil import copyfile
copyfile(src, dst)
SQLite
sqlite3-DB-API 2.0 interface for SQLite databases https://docs.python.org/2/library/sqlite3.html
copyfile(src, dst)
A minimal SQLite shell for experiments
#Inserting data
import sqlite3 as lite
import sys
con = lite.connect('test.db')
with con:
cur = con.cursor()
cur.execute("CREATE TABLE Cars(Id INT, Name TEXT, Price INT)")
cur.execute("INSERT INTO Cars VALUES(1,'Audi',52642)")
cur.execute("INSERT INTO Cars VALUES(2,'Mercedes',57127)")
cur.execute("INSERT INTO Cars VALUES(3,'Skoda',9000)")
cur.execute("INSERT INTO Cars VALUES(4,'Volvo',29000)")
cur.execute("INSERT INTO Cars VALUES(5,'Bentley',350000)")
cur.execute("INSERT INTO Cars VALUES(6,'Citroen',21000)")
cur.execute("INSERT INTO Cars VALUES(7,'Hummer',41400)")
cur.execute("INSERT INTO Cars VALUES(8,'Volkswagen',21600)")
#Retrieving data
import sqlite3 as lite
import sys
con = lite.connect('test.db')
with con:
cur = con.cursor()
cur.execute("SELECT * FROM Cars")
rows = cur.fetchall()
for row in rows:
print (row)