###json
#文件1
import json
dic = {
‘a‘:[11,22,33],
‘b‘:{‘c‘:[‘a1‘,‘a2‘],1:‘dd‘}
}
newdic = json.dumps(dic)
f = open(‘jsondic.txt‘,‘wb‘)
f.write(newdic)
f.close()
#文件2
import json
f = open(‘jsondic.txt‘,‘rb‘)
name = json.loads(f.read())
f.close()
print name
print name[‘b‘][‘1‘]
json是处理对不同端数据经行序列化转换,包括内存数据。处理常用数据类型。所有语言都支持
###pickle
功能与json相同 但处理数据类型更丰富些 只有python有
#文件1
import pickle
import datetime
dic = {
‘a‘:datetime.datetime.now(),
‘b‘:{‘c‘:[‘a1‘,‘a2‘],1:‘dd‘}
}
f = open(‘jsondic.txt‘,‘wb‘)
pickle.dump(dic,f)
f.close()
#文件2
import pickle
f = open(‘jsondic.txt‘,‘rb‘)
name = pickle.load(f)
f.close()
print name
print name[‘b‘][1]
###subprocess
返回执行结果状态
call 命令正确执行返回0 错误返回1
check_call 命令正确执行返回0 错误返回异常信息
返回执行结果内容
check_output 可以将命令执行过程结果赋值给变量
查看执行结果内容
communicate()
单独启动一个环境执行
t = subprocess.Popen(["python"],stdin=subprocess.PIPE,stdout=subprocessIPE,stderr=subprocess.PIPE)
###shutil
复制和压缩文件或目录
###time datetime
time.strftime(‘%Y-%m-%d %H:%S‘)
t = time.strptime("2015-9-19 19:23","%Y-%m-%d %H:%S")
time.mktime(t) #转换为时间戳
###logging
import logging
logger = logging.getLogger(‘simple_example‘) #对应日志格式化里name变量,如程序执行用户或者ip
logger.setLevel(logging.DEBUG) #定义全局日志级别为DEBUG
# create file handler which logs even debug messages
fh = logging.FileHandler(‘spam.log‘)
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
# ‘application‘ code
logger.debug(‘debug message‘)
logger.info(‘info message‘)
logger.warn(‘warn message‘)
logger.error(‘error message‘)
logger.critical(‘critical message‘)
###re
python的正则表达式模块
match
search
findall
split
sub
search
###面向对象
class people(object):
def __init__(self,name):
self.name = name
def say_name(self):
print self.name
def bother(self):
print self.name
a1 = people("a")
a2 = people("b")
a1.say_name()
面向对象编程的作用:重用,节省,继承,多态
什么是对象?a1 a2即是对象,star为实例,a1 = people("earth")是实例化的一个过程,当进行实例化的时候,a1会被代入类people中的self参数,用来声明是谁在调用。可以有很多的对象,people可以分别的给多个对象使用,并实现功能重用。
def __init__(self,name):
self.name = name
这个是初始化函数,只要有实例化过程,就会执行,这里的self.name = name变量设置是必要的,将局部变量设置为全局变量,给指定的对象设置变量,a1.say_name()的执行即使执行people.say_name(‘a1‘).
原文:http://www.cnblogs.com/plzros/p/5016855.html