Python - ConfigParser - AttributeError: ConfigParser 实例没有属性“__getitem__”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16407329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:32:09  来源:igfitidea点击:

Python - ConfigParser - AttributeError: ConfigParser instance has no attribute '__getitem__'

pythoniniconfigparser

提问by Igor

I am creating a quote of the day server. I am reading options from an INI file, whose text is below:

我正在创建当天服务器的报价。我正在从 INI 文件中读取选项,其文本如下:

[Server]
host =
port = 17

[Quotes]
file=quotes.txt

However, when I use ConfigParser, it gives me this error:

但是,当我使用 ConfigParser 时,它给了我这个错误:

Traceback (most recent call last):
  File "server.py", line 59, in <module>
    Start()
  File "server.py", line 55, in Start
    configOptions = parseConfig(filename)
  File "server.py", line 33, in parseConfig
    server = config['Server']
AttributeError: ConfigParser instance has no attribute '__getitem__'

Here is my code:

这是我的代码:

#!/usr/bin/python

from socket import *
from  ConfigParser import *
import sys

class serverConf:
    port = 17
    host = ""
    quotefile = ""

def initConfig(filename):


    config = ConfigParser()

    config['Server'] = {'port': '17', 'host': ''}
    config['Quotes'] = {'file': 'quotes.txt'}

    with open(filename, 'w') as configfile:
        config.write(configfile)


def parseConfig(filename):

    configOptions = serverConf()



    config = ConfigParser()
    config.read(filename)

    server = config['Server']

    configOptions.port = int(server['port'])
    configOptions.host = conifg['Server']['host']
    configOptions.quoteFile = config['Quotes']['file']



    print "[Info] Read configuration options"

    return configOptions

def doInitMessage():

    print "Quote Of The Day Server"
    print "-----------------------"
    print "Version 1.0 By Ian Duncan"
    print ""

def Start():

    filename = "qotdconf.ini"
    configOptions = parseConfig(filename)

    print "[Info] Will start server at: " + configOptions.host + ":" + configOptions.port

Start()

Why am I getting this error, and what can I do to fix it?

为什么我会收到此错误,我该怎么做才能修复它?

采纳答案by JHolta

After a quick read it seems like you're trying to read the data as if it's a dictionary, when you should use: config.get(section, data)

快速阅读后,您似乎正在尝试将数据当作字典来读取,这时您应该使用: config.get(section, data)

EG:

例如:

...
config = ConfigParser()
config.read(filename)
...
configOptions.port = config.getint('Server', 'port')
configOptions.host = config.get('Server', 'host')
configOptions.quoteFile = config.get('Quotes', 'file')

To write to the config-file you could do something like:

要写入配置文件,您可以执行以下操作:

...
def setValue(parser, sect, index, value):
    cfgfile = open(filename, 'w')
    parser.set(sect, index, value)
    parser.write(cfgfile)
    cfgfile.close()

回答by Marc

The included ConfigParserwith python 2.7 does not work in this fashion. You can, however, achieve exactly what you've proposed using the back ported configparsermodule available on PyPy.

ConfigParserpython 2.7 中包含的不以这种方式工作。但是,您可以使用PyPy 上提供的向后移植configparser模块完全实现您的建议。

pip install configparser

Then you can use it just as you would in Python 3*

然后您可以像在 Python 3* 中一样使用它

from configparser import ConfigParser
parser = ConfigParser()
parser.read("settings.ini")
# or parser.read_file(open("settings.ini"))
parser['Server']['port']
# '17'
parser.getint('Server', 'port')
#  17

NOTE

笔记

  • configparseris not 100% compatible with the Python 3 version.
  • The backport is intended to keep 100% compatibility with the vanilla release in Python 3.2+.
  • Using it in this fashion displayed above, will default to the Python 3 implementation if available.
  • configparser与 Python 3 版本不是 100% 兼容。
  • 向后移植旨在与 Python 3.2+ 中的 vanilla 版本保持 100% 的兼容性。
  • 以上面显示的这种方式使用它,如果可用,将默认为 Python 3 实现。