一,pickle模块使用的数据格式是python专用的,并且不同版本不向后兼容,同时也不能被其他语言说识别。用于将数据方便地从文件或网络存取。示例如下:
#!/usr/bin/python import pickle def save_data(file, data): pickle.dump(data, open(file, 'w')) def load_data(file): return pickle.load(open(file, 'r')) if __name__ == '__main__': data = {} data[0] = "apple" data["name"] = "ciaos" print data save_data("save.dat", data) loaddata = load_data("save.dat") print loaddata
执行效果如下:
root # python test.py {0: 'apple', 'name': 'ciaos'} {0: 'apple', 'name': 'ciaos'}
二,ConfigParser解析的配置文件的格式比较象ini的配置文件格式,就是文件中由多个section构成,每个section下又有多个配置项
root # cat test.conf [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass=password [concurrent] thread=10 processor=20
#test.py #!/usr/bin/python import ConfigParser import string, os, sys cf = ConfigParser.ConfigParser() cf.read("test.conf") # return section s = cf.sections() print 'section:', s o = cf.options("db") print 'options:', o v = cf.items("db") print 'db:', v print '-'*60 db_host = cf.get("db", "db_host") db_port = cf.getint("db", "db_port") db_user = cf.get("db", "db_user") db_pass = cf.get("db", "db_pass") threads = cf.getint("concurrent", "thread") processors = cf.getint("concurrent", "processor") print "db_host:", db_host print "db_port:", db_port print "db_user:", db_user print "db_pass:", db_pass print "thread:", threads print "processor:", processors cf.set("db", "db_pass", "zhaowei") cf.write(open("test.conf", "w"))
执行效果如下:
root # python test.py section: ['db', 'concurrent'] options: ['db_host', 'db_port', 'db_user', 'db_pass'] db: [('db_host', '127.0.0.1'), ('db_port', '3306'), ('db_user', 'root'), ('db_pass', 'password')] ------------------------------------------------------------ db_host: 127.0.0.1 db_port: 3306 db_user: root db_pass: password thread: 10 processor: 20
三,python提供了一个日志模块logging,它可以把我们想要的信息全部保存到一个日志文件中,方面我们查看
import logging def get_logger(log_name): logger = logging.getLogger() formatter = logging.Formatter('%(pathname)s - %(asctime)s - %(name)s - %(levelname)s - %(message)s',) file_handler = logging.FileHandler(log_name) file_handler.setFormatter(formatter) logger.addHandler(file_handler) logger.setLevel(logging.NOTSET) return logger logger = get_logger("file.log") logger.debug('debug') logger.info('info') logger.warning('warn') logger.error('error') # # root # cat test.log # test.py - 2013-05-28 23:59:22,771 - root - ERROR - test one # test.py - 2013-05-28 23:59:22,771 - root - ERROR - test two #
四,import pdb包用于代码调试,在需要设置断点的地方添加pdb.set_trace()
参照http://blog.csdn.net/luckeryin/article/details/4477233
五,python脚本中若包含中文字符,运行会出错,可以在源代码文件前面添加如下代码即可
#!/usr/bin/python # -*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf8')
六,post表单
import urllib import urllib2 postdata = {"name":"test"} f = urllib2.urlopen( url = "http://127.0.0.1/post.php", data = urllib.urlencode(postdata) ) print f.read()
七,利用urllib2实现文件下载
def downloader(filename, url): try: f = urllib2.urlopen(url,timeout=10800) with open(filename, "wb") as code: code.write(f.read()) except: logger.critical(sys.exc_info()) logger.error("download error " + url) return 0 logger.info("download ok " + url) return 1