Python论坛  - 讨论区

标题:Re: Re: [python-chinese] PyINI- .ini文件的解析库

2005年02月18日 星期五 14:35

limodou limodou at gmail.com
Fri Feb 18 14:35:55 HKT 2005

南方下雨,北方下雪呗 :)


On Fri, 18 Feb 2005 14:25:18 +0800, aidiseng <aidiseng100 at 163.com> wrote:
> limodou,您好!
> 
>         下雨  ?
> 你是哪的呀 我这下雪呀 哈
> 
> ======= 2005-02-12 15:04:00 您在来信中写道:=======
> 
> >python已经提供了这样的标准库,ConfigParser可以参考。
> >
> >SeSe wrote:
> >> 大年初一下雨,灭的出去,就用Python写了这么个东西,
> >> 其实我需要的是C++的ini parser库,libini看起来不
> >> 爽,所以自己用Python写了一个,然后用C++来调用,
> >> 因此里面有些函数很奇怪,比如数据类型转换。。那是
> >> 给C++ code用的,Python自己不用。C++代码稍后贴
> >>
> >> Licenced under BSD Licence,嗯嗯,新年rp ++
> >>
> >> 大家新年好!
> >>
> >>
> >>
> >> # Copyright (c) 2005 Huacheng Ke <sese at 263.net>
> >> # All rights reserved.
> >>
> >> # Redistribution and use in source and binary forms, with or without
> >> # modification, are permitted provided that the following conditions
> >> # are met:
> >> # 1. Redistributions of source code must retain the above copyright
> >> # notice, this list of conditions and the following disclaimer.
> >> # 2. Redistributions in binary form must reproduce the above copyright
> >> # notice, this list of conditions and the following disclaimer in the
> >> # documentation and/or other materials provided with the distribution.
> >> # 3. The name of the author may not be used to endorse or promote products
> >> # derived from this software without specific prior written permission.
> >> #
> >> # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
> >> # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
> >> # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
> >> # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
> >> # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
> >> # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
> >> # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
> >> # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> >> # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
> >> # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> >>
> >> import string
> >> import re
> >> import types
> >>
> >> INI_BOOL = 1
> >> INI_INTEGER = 2
> >> INI_DOUBLE = 3
> >> INI_STRING = 4
> >>
> >> INI_EXIST = 0
> >> INI_NO_SECTION = 1
> >> INI_NO_KEY = 2
> >>
> >> class INI_File:
> >> def __init__(self):
> >> self.__buffer = {}
> >>
> >> def test_exist(self, section, key = ''):
> >> if not self.__buffer.has_key(section):
> >> return INI_NO_SECTION
> >> elif key == '':
> >> return INI_EXIST
> >> elif self.__buffer[section].has_key(key):
> >> return INI_EXIST
> >> else:
> >> return INI_NO_KEY
> >>
> >> def get_type(self, str):
> >> str = str.strip()
> >> assert(self.count_element(str) == 1)
> >> try:
> >> string.atoi(str)
> >> except ValueError:
> >> try:
> >> string.atof(str)
> >> except ValueError:
> >> return INI_STRING
> >> return INI_DOUBLE
> >> int_value = string.atoi(str)
> >> if int_value == 1 or int_value == 0:
> >> return INI_BOOL
> >> else:
> >> return INI_INTEGER
> >>
> >> def count_element(self, str):
> >> str = str.strip()
> >> segments = string.split(str, ',')
> >> return len(segments)
> >>
> >> def get_sub_string(self, str, index):
> >> str = str.strip()
> >> assert(self.count_element(str) > 1)
> >> segments = string.split(str, ',')
> >> assert(index < len(segments))
> >> return segments[index]
> >>
> >> def to_int(self, str):
> >> assert(self.get_type(str) <= INI_INTEGER)
> >> return string.atoi(str)
> >>
> >> def to_bool(self, str):
> >> assert(self.get_type(str) == INI_BOOL)
> >> return string.atoi(str)
> >>
> >> def to_double(self, str):
> >> assert(self.get_type(str) <= INI_DOUBLE)
> >> return string.atof(str)
> >>
> >> def retrieve_string(self, section, key):
> >> assert(self.test_exist(section, key) == INI_EXIST)
> >> return self.__buffer[section][key]
> >>
> >> def start_section(self, section):
> >> # section == '' indicates the global section
> >> self.__buffer[section] = {}
> >>
> >> def insert_item(self, section, key, value):
> >> self.__buffer[section][key] = value
> >> def clear(self):
> >> self.__buffer = {}
> >> def print_buffer(self):
> >> print self.__buffer
> >>
> >> class INI_Reader:
> >> def __init__(self):
> >> self.verbose = False
> >> def toggle_verbose(self, val):
> >> self.verbose = val
> >> def read(self, filename):
> >> try:
> >> f = open(filename)
> >> lines = f.readlines()
> >> lines = [line.rstrip('\n') for line in lines]
> >> lines = [line.strip() for line in lines]
> >> f.close()
> >> except IOError:
> >> lines = []
> >> return None
> >> print "PyINI: Parsing INI file : " + filename
> >> result = INI_File()
> >> section_pattern = re.compile('\[\w*\]')
> >> section = ''
> >> result.start_section('')
> >> for line in lines:
> >> if line == '':
> >> continue
> >> match_result = section_pattern.match(line)
> >> if match_result:
> >> # this line is a section tag
> >> section = match_result.group(0).strip('\[\]')
> >> result.start_section(section)
> >> else:
> >> segments = string.split(line, ';')
> >> line = segments[0].strip()
> >> if line != '':
> >> segments = string.split(line, "=")
> >> segments = [ segment.strip() for segment in segments]
> >> if len(segments) == 2:
> >> key = segments[0]
> >> value = segments[1]
> >> if self.verbose:
> >> print " [" + section + "] : <" + key + '> = <' + value + '>'
> >> result.insert_item(section, key, value)
> >> else:
> >> print "PyINI: Invalid line - " + segments[0]
> >> print "Powered by PyINI, http://pyini.sourceforge.net/"
> >> return result
> >>
> >> if __name__ == "__main__":
> >> reader = INI_Reader()
> >> result = reader.read("1.ini")
> >> result.print_buffer()
> >> print result.test_exist('MAIN','e')
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>>
> >>>
> >>>
> >>
> >>
> >> _______________________________________________
> >> python-chinese list
> >> python-chinese at lists.python.cn
> >> http://python.cn/mailman/listinfo/python-chinese
> >>
> >
> >--
> >I love python!
> >My Blog: http://www.donews.net/limodou
> >_______________________________________________
> >python-chinese list
> >python-chinese at lists.python.cn
> >http://python.cn/mailman/listinfo/python-chinese
> 
> = = = = = = = = = = = = = = = = = = = =
> 
>> 礼!
> 
> 
> aidiseng
> aidiseng100 at 163.com
> 2005-02-18
> 
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 


-- 
I like python! 
My Blog: http://www.donews.net/limodou
New Maillist: http://groups-beta.google.com/group/python-cn


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年02月18日 星期五 14:46

Tintin genedna at gmail.com
Fri Feb 18 14:46:51 HKT 2005

据说杭州下雨了。


On Fri, 18 Feb 2005 14:35:55 +0800, limodou <limodou at gmail.com> wrote:
> 南方下雨,北方下雪呗 :)
> 
> 
> On Fri, 18 Feb 2005 14:25:18 +0800, aidiseng <aidiseng100 at 163.com> wrote:
> > limodou,您好!
> >
> >         下雨  ?
> > 你是哪的呀 我这下雪呀 哈
> >
> > ======= 2005-02-12 15:04:00 您在来信中写道:=======
> >
> > >python已经提供了这样的标准库,ConfigParser可以参考。
> > >
> > >SeSe wrote:
> > >> 大年初一下雨,灭的出去,就用Python写了这么个东西,
> > >> 其实我需要的是C++的ini parser库,libini看起来不
> > >> 爽,所以自己用Python写了一个,然后用C++来调用,
> > >> 因此里面有些函数很奇怪,比如数据类型转换。。那是
> > >> 给C++ code用的,Python自己不用。C++代码稍后贴
> > >>
> > >> Licenced under BSD Licence,嗯嗯,新年rp ++
> > >>
> > >> 大家新年好!
> > >>
> > >>
> > >>
> > >> # Copyright (c) 2005 Huacheng Ke <sese at 263.net>
> > >> # All rights reserved.
> > >>
> > >> # Redistribution and use in source and binary forms, with or without
> > >> # modification, are permitted provided that the following conditions
> > >> # are met:
> > >> # 1. Redistributions of source code must retain the above copyright
> > >> # notice, this list of conditions and the following disclaimer.
> > >> # 2. Redistributions in binary form must reproduce the above copyright
> > >> # notice, this list of conditions and the following disclaimer in the
> > >> # documentation and/or other materials provided with the distribution.
> > >> # 3. The name of the author may not be used to endorse or promote products
> > >> # derived from this software without specific prior written permission.
> > >> #
> > >> # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
> > >> # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
> > >> # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
> > >> # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
> > >> # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
> > >> # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
> > >> # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
> > >> # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> > >> # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
> > >> # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> > >>
> > >> import string
> > >> import re
> > >> import types
> > >>
> > >> INI_BOOL = 1
> > >> INI_INTEGER = 2
> > >> INI_DOUBLE = 3
> > >> INI_STRING = 4
> > >>
> > >> INI_EXIST = 0
> > >> INI_NO_SECTION = 1
> > >> INI_NO_KEY = 2
> > >>
> > >> class INI_File:
> > >> def __init__(self):
> > >> self.__buffer = {}
> > >>
> > >> def test_exist(self, section, key = ''):
> > >> if not self.__buffer.has_key(section):
> > >> return INI_NO_SECTION
> > >> elif key == '':
> > >> return INI_EXIST
> > >> elif self.__buffer[section].has_key(key):
> > >> return INI_EXIST
> > >> else:
> > >> return INI_NO_KEY
> > >>
> > >> def get_type(self, str):
> > >> str = str.strip()
> > >> assert(self.count_element(str) == 1)
> > >> try:
> > >> string.atoi(str)
> > >> except ValueError:
> > >> try:
> > >> string.atof(str)
> > >> except ValueError:
> > >> return INI_STRING
> > >> return INI_DOUBLE
> > >> int_value = string.atoi(str)
> > >> if int_value == 1 or int_value == 0:
> > >> return INI_BOOL
> > >> else:
> > >> return INI_INTEGER
> > >>
> > >> def count_element(self, str):
> > >> str = str.strip()
> > >> segments = string.split(str, ',')
> > >> return len(segments)
> > >>
> > >> def get_sub_string(self, str, index):
> > >> str = str.strip()
> > >> assert(self.count_element(str) > 1)
> > >> segments = string.split(str, ',')
> > >> assert(index < len(segments))
> > >> return segments[index]
> > >>
> > >> def to_int(self, str):
> > >> assert(self.get_type(str) <= INI_INTEGER)
> > >> return string.atoi(str)
> > >>
> > >> def to_bool(self, str):
> > >> assert(self.get_type(str) == INI_BOOL)
> > >> return string.atoi(str)
> > >>
> > >> def to_double(self, str):
> > >> assert(self.get_type(str) <= INI_DOUBLE)
> > >> return string.atof(str)
> > >>
> > >> def retrieve_string(self, section, key):
> > >> assert(self.test_exist(section, key) == INI_EXIST)
> > >> return self.__buffer[section][key]
> > >>
> > >> def start_section(self, section):
> > >> # section == '' indicates the global section
> > >> self.__buffer[section] = {}
> > >>
> > >> def insert_item(self, section, key, value):
> > >> self.__buffer[section][key] = value
> > >> def clear(self):
> > >> self.__buffer = {}
> > >> def print_buffer(self):
> > >> print self.__buffer
> > >>
> > >> class INI_Reader:
> > >> def __init__(self):
> > >> self.verbose = False
> > >> def toggle_verbose(self, val):
> > >> self.verbose = val
> > >> def read(self, filename):
> > >> try:
> > >> f = open(filename)
> > >> lines = f.readlines()
> > >> lines = [line.rstrip('\n') for line in lines]
> > >> lines = [line.strip() for line in lines]
> > >> f.close()
> > >> except IOError:
> > >> lines = []
> > >> return None
> > >> print "PyINI: Parsing INI file : " + filename
> > >> result = INI_File()
> > >> section_pattern = re.compile('\[\w*\]')
> > >> section = ''
> > >> result.start_section('')
> > >> for line in lines:
> > >> if line == '':
> > >> continue
> > >> match_result = section_pattern.match(line)
> > >> if match_result:
> > >> # this line is a section tag
> > >> section = match_result.group(0).strip('\[\]')
> > >> result.start_section(section)
> > >> else:
> > >> segments = string.split(line, ';')
> > >> line = segments[0].strip()
> > >> if line != '':
> > >> segments = string.split(line, "=")
> > >> segments = [ segment.strip() for segment in segments]
> > >> if len(segments) == 2:
> > >> key = segments[0]
> > >> value = segments[1]
> > >> if self.verbose:
> > >> print " [" + section + "] : <" + key + '> = <' + value + '>'
> > >> result.insert_item(section, key, value)
> > >> else:
> > >> print "PyINI: Invalid line - " + segments[0]
> > >> print "Powered by PyINI, http://pyini.sourceforge.net/"
> > >> return result
> > >>
> > >> if __name__ == "__main__":
> > >> reader = INI_Reader()
> > >> result = reader.read("1.ini")
> > >> result.print_buffer()
> > >> print result.test_exist('MAIN','e')
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>>
> > >>>
> > >>>
> > >>
> > >>
> > >> _______________________________________________
> > >> python-chinese list
> > >> python-chinese at lists.python.cn
> > >> http://python.cn/mailman/listinfo/python-chinese
> > >>
> > >
> > >--
> > >I love python!
> > >My Blog: http://www.donews.net/limodou
> > >_______________________________________________
> > >python-chinese list
> > >python-chinese at lists.python.cn
> > >http://python.cn/mailman/listinfo/python-chinese
> >
> > = = = = = = = = = = = = = = = = = = = =
> >
> > 致
> > 礼!
> >
> >
> > aidiseng
> > aidiseng100 at 163.com
> > 2005-02-18
> >
> >
> > _______________________________________________
> > python-chinese list
> > python-chinese at lists.python.cn
> > http://python.cn/mailman/listinfo/python-chinese
> >
> 
> --
> I like python!
> My Blog: http://www.donews.net/limodou
> New Maillist: http://groups-beta.google.com/group/python-cn
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 


-- 
欢迎访问我的Blog:
http://spaces.msn.com/members/meaglith/


----------------------------------------
       .--.
      |o_o |
      |:_/ |
     //   \ \
    (|     | )
   /'\_   _/`\
   \___)=(___/
-------------------------------------------------
沧海笑 滔滔两岸潮 浮沉随浪记今朝 
苍天笑 纷纷世上潮 谁负谁胜天知晓 
江山笑 烟两遥 涛浪淘尽红尘俗也知多少 
竟惹寂寥 一襟晚照 
苍生笑 不再寂寥 豪情仍在痴痴笑笑


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号