1. 读配置文件
为了更好的维护程序,提高程序的灵活性,往往使用配置文件来设置程序中的变量。
config文件
[global]
ip=10.3.22.26
port=123
user=user
password=user
读取
import ConfigParser
config1 = ConfigParser.ConfigParser()
config1.readfp(open("config","rb"))
print config1.get("global","ip")
2. 正则表达式处理字符串
替换字符串中特定格式的子串
import re
pattern=re.compile('\[\d*\]') #[1],[1234]此类格式
str='[1]123 [2]245 [3]789'
#去掉[...]
str1=pattern.sub('',str)
以特定格式子串为分隔符,分割字符串,返回列表
arr=re.split('\[\d*\]',str1)
import time
time.strftime('%Y-%d-%x', time.localtime())