pythonÄ£¿éÖ®ConfigParser: ÓÃpython½âÎöÅäÖÃÎļþ
ÔÚ³ÌÐòÖÐʹÓÃÅäÖÃÎļþÀ´Áé»îµÄÅäÖÃһЩ²ÎÊýÊÇÒ»¼þºÜ³£¼ûµÄÊÂÇ飬ÅäÖÃÎļþµÄ½âÎö²¢²»¸´ÔÓ£¬ÔÚpythonÀï¸üÊÇÈç´Ë£¬ÔÚ¹Ù·½·¢²¼µÄ¿âÖоͰüº¬ÓÐ×öÕâ¼þÊÂÇé
µÄ¿â£¬ÄǾÍÊÇConfigParser£¬ÕâÀï¼òµ¥µÄ×öһЩ½éÉÜ¡£
ConfigParser½âÎöµÄÅäÖÃÎļþµÄ¸ñʽ±È½ÏÏóiniµÄÅäÖÃÎļþ¸ñʽ£¬¾ÍÊÇÎļþÖÐÓɶà¸ösection¹¹³É£¬Ã¿¸ösectionÏÂÓÖÓжà¸öÅäÖÃÏ
±ÈÈ磺
[db]
db_host=127.0.0.1
db_port=3306
db_user=root
db_pass=password
[concurrent]
thread=10
processor=20
¼ÙÉèÉÏÃæµÄÅäÖÃÎļþµÄÃû×ÖΪtest.conf¡£ÀïÃæ°üº¬Á½¸ösection,Ò»¸öÊÇdb, ÁíÒ»¸öÊÇconcurrent,
dbÀïÃ滹°üº¬ÓÐ4ÏconcurrentÀïÃæÓÐÁ½Ïî¡£ÕâÀïÀ´×ö×ö½âÎö£º
#-*- encoding: gb2312 -*-
import ConfigParser
import string, os, sys
cf = ConfigParser.ConfigParser()
cf.read("test.conf")
# ·µ»ØËùÓеÄ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"))
Ïà¹ØÎĵµ£º
PythonÊÇÄ¿Ç°¹ã·ºÊ¹ÓõÄÒ»ÃŶ¯Ì¬ÓïÑÔ£¬ÀàËÆJava£¬Ô´´úÂë±ØÐëÊ×ÏÈÓɱàÒëÆ÷ת»»³É×Ö½ÚÂ루byte code£©£¬È»ºóÔÙÓɽâÊÍÆ÷À´Ö´ÐÐ×Ö½ÚÂë¡£ÓëJava²»Í¬µÄÊÇ£¬PythonµÄ±àÒëÆ÷ºÍ½âÊÍÆ÷¶¼ÊÇÒ»¸ö³ÌÐò¡£Òò´Ë£¬Ô´´úÂëÒ²¿ÉÒÔÖ±½Ó½»¸øÕâ¸ö±àÒëÆ÷£¯½âÊÍÆ÷À´Ö´ÐС£
ʹÓÃPython±àд³ÌÐò£¬Äã²»ÐèÒªÔ¤ÏÈÉùÃ÷ºÃ±äÁ¿µÄÀàÐÍ£¬ÒòΪPythonÖбäÁ¿µÄÀ ......
Éè¼ÆģʽÊÇÒ»¸ö³éÏó²ã´Î£¬ÃèÊöÁËÔÚÒ»¸öÌض¨µÄ»·¾³ÖÐÓÃÀ´½â¾öÒ»°ãÉè¼ÆÎÊÌâµÄ¶ÔÏóºÍÀàÖ®¼äµÄ½»»¥¹Øϵ£¬ÆäÖ÷ҪĿµÄÊdzä·ÖÀûÓÃÓïÑÔµÄÌØÐÔ£¬Éè¼Æ¿É¸´Óõġ¢Äܹ»ÊÊÓ¦ÐèÇó±ä¸üµÄÈí¼þ[9]¡£Éè¼ÆģʽÊÇÒ»ÖÖÉè¼Æ˼Ï룬ÓïÑÔÊÇʵÏÖ˼ÏëµÄ¹¤¾ß¡£Òò´Ë£¬²»Í¬ÓïÑÔµÄÌØÐÔÓ°ÏìÁËÉè¼ÆģʽµÄʵÏÖ£¬ÓÐЩÓïÑÔ¸üÈÝÒ×ʵÏÖÉè¼Æģʽ£¬¶øÓÐЩÓïÑÔÔ ......
ΪÁËÑ¡ÔñÒ»¸öºÏÊʵĽű¾ÓïÑÔѧϰ£¬½ñÌì²éÁ˲»ÉÙÓйØPerl£¬Python£¬Ruby£¬JavascriptµÄ¶«Î÷£¬¿ÉÊÇ·¢ÏÖ¸÷´óÕóÓªµÄÈ˶¼ÔÚ´µÅõ×Ô¼ºÏ²»¶µÄÓïÑÔ£¬²»¹ý×îûÓÐÕùÒéµÄÓ¦¸ÃÊÇJavascriptÏֽ׶λ¹²»ÊʺÏÓÃÀ´×ö¶ÀÁ¢¿ª·¢£¬ËüµÄÌìÏ»¹ÊÇÔÚwebÓ¦ÓÃÉÏ¡£ ÎÒÖ÷ÒªÊÇÏë×öÊý¾ÝÍÚ¾òËã·¨µÄÑо¿£¬Ó¦¸Ã»á´¦Àí´óÁ¿µÄÎı¾¡£Ìáµ½Îı¾´¦Àí£¬ÏàÐŴ󲿷ÖÈË ......
ÔÎÄ
http://www.hetland.org/python/instant-hacking.php
Instant Hacking[Òë
ÎÄ]
ÒëÕߣº ¿Ï¶¨À´¹ý   ......