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

大兔子大兔子 提交于 2020-01-01 05:06:05

问题


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

[Server]
host =
port = 17

[Quotes]
file=quotes.txt

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

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?


回答1:


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)

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()



回答2:


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

pip install configparser

Then you can use it just as you would in 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

  • configparser is 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.


来源:https://stackoverflow.com/questions/16407329/python-configparser-attributeerror-configparser-instance-has-no-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!