获取当前时间
import datetime
print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
命令行参数
import sys
sys.argv[1]
文件按行读入数组,或整个文件读入一个字符串变量
line_list = [ line.rstrip('\n').rstrip('\r') for line in open(fname) ]
with open(fname) as myfile:
str=myfile.read()
文件写入
f = open(out_fname,'w')
f.write(some_string + "\n")
f.close()
解析html,解析table表格
from bs4 import BeautifulSoup
soup = BeautifulSoup(open(html_fname))
tbodys = soup.findChildren('tbody')
tbody = tbodys[0]
rows = tbody.findChildren('tr')
tb = []
for row in rows:
r = []
cells = row.findChildren('td')
for cell in cells:
value = cell.string.strip()
r.append(value)
tb.append(r)
#tb is table cell dim2 array
字符匹配
import re
m1 = re.search(':', str)
if m1:
print "string " + str + " contains : "
打印字符串,类似 sprintf
res = "%-10s%10s%5s" % ( str_a, str_b, str_c )
删除文件
os.remove(fname)
调用shell命令
from subprocess import call
call(["ls", "-l"])
定义函数
def somefun(x):
y = x*x
return y
多进程
from multiprocessing import Pool
pool = Pool(processes=proc_number)
result_list = pool.map(some_fun, task_list)
文件是否存在
if os.path.isfile(fname):
print "exists file : " + fname
没有评论:
发表评论