【问题标题】:Handling duplicate keys with ConfigParser [duplicate]使用 ConfigParser 处理重复键 [重复]
【发布时间】:2012-12-17 20:03:22
【问题描述】:

可能重复:
Python Config Parser (Duplicate Key Support)

我正在尝试用 Python 读取 INI 格式的项目文件。该文件在一个部分中包含重复的键(具有唯一值)。例如,其中一个部分如下所示:

[Source Files]
Source="file1.c"
Source="file2.c"
Source="file3.c"

如果我使用以下代码阅读本文

config = configparser.ConfigParser( strict=False )
config.read( "project/file/name" )
print( config.get( "Source Files", "Source" ) )

结果是

"file3.c"

有没有办法获取键 Source 的所有值的列表?我愿意使用其他方法来解析文件。

请注意,我无法更改文件格式。

【问题讨论】:

    标签: python python-3.x configparser


    【解决方案1】:

    我最终继承了RawConfigParser 类来实现这个特性。如果其他人对此感兴趣,这是我正在使用的代码:

    import configparser
    
    class ConfigParserMultiOpt(configparser.RawConfigParser):
      """ConfigParser allowing duplicate keys. Values are stored in a list"""
    
      def __init__(self):
        configparser.RawConfigParser.__init__(self, empty_lines_in_values=False, strict=False)
    
      def _read(self, fp, fpname):
        """Parse a sectioned configuration file.
    
        Each section in a configuration file contains a header, indicated by
        a name in square brackets (`[]'), plus key/value options, indicated by
        `name' and `value' delimited with a specific substring (`=' or `:' by
        default).
    
        Values can span multiple lines, as long as they are indented deeper
        than the first line of the value. Depending on the parser's mode, blank
        lines may be treated as parts of multiline values or ignored.
    
        Configuration files may include comments, prefixed by specific
        characters (`#' and `;' by default). Comments may appear on their own
        in an otherwise empty line or may be entered in lines holding values or
        section names.
        """
        elements_added = set()
        cursect = None                        # None, or a dictionary
        sectname = None
        optname = None
        lineno = 0
        indent_level = 0
        e = None                              # None, or an exception
        for lineno, line in enumerate(fp, start=1):
          comment_start = None
          # strip inline comments
          for prefix in self._inline_comment_prefixes:
            index = line.find(prefix)
            if index == 0 or (index > 0 and line[index-1].isspace()):
              comment_start = index
              break
          # strip full line comments
          for prefix in self._comment_prefixes:
            if line.strip().startswith(prefix):
              comment_start = 0
              break
          value = line[:comment_start].strip()
          if not value:
            if self._empty_lines_in_values:
              # add empty line to the value, but only if there was no
              # comment on the line
              if (comment_start is None and
                  cursect is not None and
                  optname and
                  cursect[optname] is not None):
                  cursect[optname].append('') # newlines added at join
            else:
              # empty line marks end of value
              indent_level = sys.maxsize
            continue
          # continuation line?
          first_nonspace = self.NONSPACECRE.search(line)
          cur_indent_level = first_nonspace.start() if first_nonspace else 0
          if (cursect is not None and optname and
              cur_indent_level > indent_level):
              cursect[optname].append(value)
          # a section header or option header?
          else:
            indent_level = cur_indent_level
            # is it a section header?
            mo = self.SECTCRE.match(value)
            if mo:
              sectname = mo.group('header')
              if sectname in self._sections:
                if self._strict and sectname in elements_added:
                  raise DuplicateSectionError(sectname, fpname,
                                              lineno)
                cursect = self._sections[sectname]
                elements_added.add(sectname)
              elif sectname == self.default_section:
                cursect = self._defaults
              else:
                cursect = self._dict()
                self._sections[sectname] = cursect
                self._proxies[sectname] = configparser.SectionProxy(self, sectname)
                elements_added.add(sectname)
              # So sections can't start with a continuation line
              optname = None
            # no section header in the file?
            elif cursect is None:
              raise MissingSectionHeaderError(fpname, lineno, line)
            # an option line?
            else:
              mo = self._optcre.match(value)
              if mo:
                optname, vi, optval = mo.group('option', 'vi', 'value')
                if not optname:
                  e = self._handle_error(e, fpname, lineno, line)
                optname = self.optionxform(optname.rstrip())
                if (self._strict and
                  (sectname, optname) in elements_added):
                  raise configparser.DuplicateOptionError(sectname, optname, fpname, lineno)
                elements_added.add((sectname, optname))
                # This check is fine because the OPTCRE cannot
                # match if it would set optval to None
                if optval is not None:
                  optval = optval.strip()
                  # Check if this optname already exists
                  if (optname in cursect) and (cursect[optname] is not None):
                    # If it does, convert it to a tuple if it isn't already one
                    if not isinstance(cursect[optname], tuple):
                      cursect[optname] = tuple(cursect[optname])
                    cursect[optname] = cursect[optname] + tuple([optval])
                  else:
                    cursect[optname] = [optval]
                else:
                    # valueless option handling
                    cursect[optname] = None
              else:
                # a non-fatal parsing error occurred. set up the
                # exception but keep going. the exception will be
                # raised at the end of the file and will contain a
                # list of all bogus lines
                e = self._handle_error(e, fpname, lineno, line)
        # if any parsing errors occurred, raise an exception
        if e:
            raise e
        self._join_multiline_values()
    

    _read 函数是从configparser.py 复制粘贴的。我所做的唯一更改是在optval = optval.strip() 行之后添加if 条件。 ConfigParserMultiOpt 将为 tuple 的一个部分中的重复键返回多个值。


    我是 Python 新手,所以如果有人对改进上述代码有任何建议,我会全力以赴!

    【讨论】:

    • 谢谢,这正是我想要的
    • 这可能对我的一些项目有用...但我会注意到 Python 的 PEP-8 style guide 是缩进应该是每级 4 个空格。您的代码在这里混合了 2 和 4 个空格,这使得它有点难以阅读。
    猜你喜欢
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 2022-12-05
    • 2012-11-05
    • 1970-01-01
    • 2012-08-04
    相关资源
    最近更新 更多