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

Igo*_*gor 8 python ini configparser

我正在创建日间服务器的引用.我正在从INI文件中读取选项,其文本如下:

[Server]
host =
port = 17

[Quotes]
file=quotes.txt
Run Code Online (Sandbox Code Playgroud)

但是,当我使用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__'
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

#!/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()
Run Code Online (Sandbox Code Playgroud)

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

JHo*_*lta 12

快速阅读后,您似乎正在尝试读取数据,就好像它是一个字典,当您应该使用: config.get(section, data)

例如:

...
config = ConfigParser()
config.read(filename)
...
configOptions.port = config.getint('Server', 'port')
configOptions.host = config.get('Server', 'host')
configOptions.quoteFile = config.get('Quotes', 'file')
Run Code Online (Sandbox Code Playgroud)

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

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

  • 这是configparser的python 3版本和configparser的pyhton 2.7版本之间的区别.在python 3.3中,这是你通常会做的. (14认同)

Mar*_*arc 5

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

pip install configparser
Run Code Online (Sandbox Code Playgroud)

然后您可以像在 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
Run Code Online (Sandbox Code Playgroud)

笔记

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