posts - 1,  comments - 1,  trackbacks - 0
          9-7習(xí)題是要求些一個(gè)解析配置文件的類,用來解析Win32,POSIX,或著其他平臺(tái)下的配置文件,我在這里只寫了一個(gè)Win32文件的ini文件的解析
          下面是我的實(shí)現(xiàn)代碼:
            1 #coding=utf-8
            2 
            3 '''
            4 Created on 2010-7-12
            5 
            6 @author: Innate Solitary
            7 
            8 ini文件解析類
            9 '''
           10 
           11 import ConfigParser
           12 
           13 
           14 class ConfigFile(object):
           15     '''
           16     配置文件解析,處理的簡單實(shí)現(xiàn)類
           17     '''
           18 
           19 
           20     def __init__(self, file_name):
           21         '''
           22         Constructor
           23         '''
           24         self.file_name = file_name
           25         self.file = file(file_name)
           26         self.cfg = ConfigParser.ConfigParser()
           27         self.read_handle = None
           28         self.write_handle = None
           29         self.sections = []
           30         self.items = {}
           31         self.init_flag = False
           32     
           33     def initialization(self):
           34         '''
           35         初始化配置文件
           36         '''
           37         try:
           38             self.read_handle = open(self.file_name, 'r')
           39             self.write_handle = open(self.file_name, 'w')
           40             self.cfg.readfp(self.read_handle)
           41             self.sections = self.cfg.sections()
           42             for section in self.sections:
           43                 self.items[section] = self.cfg.items(section)
           44             self.init_flag = True
           45         except Exception, e:
           46             print '初始化配置文件錯(cuò)誤:', str(e)
           47             self.init_flag = False
           48         return self.init_flag
           49     
           50     def close(self):
           51         '''
           52         關(guān)閉配置文件
           53         '''
           54         self.read_handle.close()
           55         del self.file
           56         del self.read_handle
           57         del self.file_name
           58         del self.cfg
           59         del self.sections
           60         del self.items
           61     
           62     def get_value(self, section, key, default = ''):
           63         '''
           64         得到指定section下的key的值,如果沒有給其一個(gè)空字符串做為默認(rèn)值,并加入文件中
           65         '''
           66         if not self.init_flag:
           67             raise Exception('未初始化配置文件,請?jiān)谔幚砦募罢{(diào)用initialization(self)進(jìn)行配置文件初始化')
           68         
           69         
           70         if not self.cfg.has_section(section):
           71             self.cfg.add_section(section)
           72         if not self.cfg.has_option(section, key):
           73             self.cfg.set(section, key, default)
           74                 
           75         value = self.cfg.get(section, key)
           76         
           77         
           78         return value
           79     
           80     def set_value(self, section, key, value):
           81         '''
           82         設(shè)置值
           83         '''
           84         
           85         if not self.cfg.has_section(section):
           86             self.cfg.add_section(section);
           87         
           88         self.cfg.set(section, key, value)
           89     
           90     def write_to_file(self, file = None):
           91         if file != None and file.mode == 'r':
           92             raise Exception('文件模式不可寫')
           93         if file == None:
           94             self.cfg.write(self.write_handle)
           95         else:
           96             self.cfg.write(file)
           97 
           98         
           99         
          100         
          101 
          我在這里使用了ConfigParser模塊,這里我的這段代碼因?yàn)楣ぷ鞯脑蛑粚?shí)現(xiàn)了一部分,并沒有把我當(dāng)初想到的功能實(shí)現(xiàn),但是習(xí)題9-7要求的功能都實(shí)現(xiàn)了,代碼很簡單個(gè)人覺沒有什么難得所以沒有加注釋。

          posted on 2010-07-15 12:47 天獨(dú) 閱讀(389) 評論(0)  編輯  收藏 所屬分類: Python

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 潍坊市| 青冈县| 中宁县| 唐河县| 高碑店市| 德令哈市| 乌海市| 九江县| 彝良县| 高台县| 天镇县| 东宁县| 抚宁县| 永平县| 晴隆县| 恩平市| 博白县| 嵊泗县| 涞水县| 华亭县| 蕉岭县| 山东省| 烟台市| 哈巴河县| 华坪县| 龙泉市| 砚山县| 廊坊市| 汉源县| 长子县| 孟州市| 略阳县| 视频| 友谊县| 宁远县| 宜宾县| 民乐县| 长武县| 彭山县| 浦北县| 恩平市|