Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python configparser将不接受没有值的键_Python_Python 3.x_Configparser - Fatal编程技术网

Python configparser将不接受没有值的键

Python configparser将不接受没有值的键,python,python-3.x,configparser,Python,Python 3.x,Configparser,因此,我正在编写一个从配置文件中读取的脚本,我想准确地使用它来设计configparser,如下所述: 我正在使用Python 3.2.1。脚本完成后,将在Windows 2008 R2计算机上运行,使用相同版本的Python,或者假设兼容,使用当时的最新版本 #!/user/bin/env python import configparser config = configparser.ConfigParser() config.read('c:\exclude.ini') config.s

因此,我正在编写一个从配置文件中读取的脚本,我想准确地使用它来设计configparser,如下所述:

我正在使用Python 3.2.1。脚本完成后,将在Windows 2008 R2计算机上运行,使用相同版本的Python,或者假设兼容,使用当时的最新版本

#!/user/bin/env python
import configparser

config = configparser.ConfigParser()
config.read('c:\exclude.ini')
config.sections()
这可以很好地读取exclude.ini文件,除非我有一个没有键的值。考虑到我可能做错了什么,我尝试分析下面列出的示例:

每次仍会抛出以下内容:

File "C:\Python32\lib\configparser.py", line 1081, in _read
    raise e
configparser.ParsingError: Source contains parsing errors: c:\exclude.ini
    [line 20]: 'key_without_value\n'
我不知所措。。。我实际上是在复制/粘贴文档中的示例代码,以获得我正在使用的python版本,但它没有发挥应有的作用。我只能假设我遗漏了什么,因为我也找不到任何有类似问题的人。

有一个关键字参数
allow\u no\u value
,默认值为
False

试着把它设为真,我打赌它会对你有用

class RawConfigParser:
def __init__(self, defaults=None, dict_type=_default_dict,
             allow_no_value=False):
    self._dict = dict_type
    self._sections = self._dict()
    self._defaults = self._dict()
    if allow_no_value:
        self._optcre = self.OPTCRE_NV
    else:
        self._optcre = self.OPTCRE
    if defaults:
        for key, value in defaults.items():
            self._defaults[self.optionxform(key)] = value
导入配置分析器


cf=ConfigParser.ConfigParser(允许\u no\u value=True)

非常好,谢谢你。也许我应该给他们发送一条注释,建议更改他们的example.ini,以便在该部分上方添加一条注释,指出只有在构造函数更改时,该部分才起作用。我想我应该读完整的文档,但在我看来,事情的安排方式默认情况下应该是这样的。即使该部分看起来是这样的,也可以这样做:[软件]3700日志副本1.0.2 Adobe Flash Player 11 ActiveX Adobe Reader X(10.1.7)-Deutsch DotNet Framework 4.0[…]