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

问题描述 投票:0回答:4

我正在创建当日服务器的报价。我正在从 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
4个回答
14
投票

快速阅读后,您似乎正在尝试像字典一样读取数据,此时您应该使用:

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

5
投票

Python 2.7 中包含的

ConfigParser
不能以这种方式工作。但是,您可以使用 PyPy
 上提供的向后移植的 
configparser 模块来准确实现您所建议的目标。

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
     与 Python 3 版本并非 100% 兼容。 
  • 向后移植旨在与 Python 3.2+ 中的普通版本保持 100% 兼容性。
  • 以上面显示的方式使用它,将默认使用 Python 3 实现(如果可用)。

0
投票
对于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>
    

0
投票
我正在安装 pip install bip-utils 并在最后出现此错误

也尝试过 pip install configparser 但同样的错误

versioneer.py”,第 399 行,在 get_config_from_root 中 解析器 = configparser.SafeConfigParser() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError:模块“configparser”没有属性“SafeConfigParser”。您的意思是:“RawConfigParser”吗? [输出结束]

© www.soinside.com 2019 - 2024. All rights reserved.