【问题标题】:Python - ConfigParser - AttributeError: ConfigParser instance has no attribute '__getitem__'Python - ConfigParser - AttributeError:ConfigParser 实例没有属性'__getitem__'
【发布时间】:2013-04-30 17:09:46
【问题描述】:

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

[Server]
host =
port = 17

[Quotes]
file=quotes.txt

但是,当我使用 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__'

这是我的代码:

#!/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()

为什么会出现此错误,我该如何解决?

【问题讨论】:

标签: python ini configparser


【解决方案1】:

快速阅读后,您似乎正在尝试像字典一样读取数据,此时您应该使用: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')

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

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

【讨论】:

  • 我一定是看错了一些网站上的指南,因为我以为它说要使用尖括号。
  • 你可以使用 python 文档:docs.python.org/2/library/configparser.html
  • 这是python 3版本的configparser和pyhton 2.7版本的configparser的区别。在 python 3.3 中,这是你通常会做的。
  • 这非常烦人,因为它们的名称几乎相同。我的本地 python2 找到了 configparser 并毫无问题地使用它,但它使我的登台崩溃了,这让我花了一段时间才弄清楚将它重命名为 ConfigParser 而不是使用尖括号。感谢stackoverflow的帮助!
【解决方案2】:

python 2.7 中包含的ConfigParser 不能以这种方式工作。但是,您可以使用向后移植的configparser 模块available on PyPy 完全实现您的建议。

pip install configparser

然后你就可以像在 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

注意

  • configparser 不是 100% 兼容 Python 3 版本。
  • 向后移植旨在与 Python 3.2+ 中的原版版本保持 100% 的兼容性。
  • 以上面显示的这种方式使用它,将默认使用 Python 3 实现(如果可用)。

【讨论】:

    【解决方案3】:

    对于 python3
    iniparser.py

    #!/usr/bin/python3
    import configparser
    import sys
    
    config = configparser.ConfigParser()
    config.read(sys.argv[1])
    print(config[sys.argv[2]][sys.argv[3]])
    

    使用

    python iniparser.py <filepath> <section> <key>
    

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 2018-04-13
      • 2013-04-15
      • 1970-01-01
      相关资源
      最近更新 更多