मैं दिन सर्वर का उद्धरण बना रहा हूं। मैं एक INI फ़ाइल, जिसका पाठ से विकल्पों पढ़ रहा हूँ नीचे है:पायथन - कॉन्फ़िगर पार्सर - विशेषता त्रुटि: कॉन्फ़िगर पार्सर उदाहरण में कोई विशेषता नहीं है '__getitem__'
[Server]
host =
port = 17
[Quotes]
file=quotes.txt
हालांकि, जब मैं ConfigParser उपयोग करें, यह मुझे इस त्रुटि देता है:
#!/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()
:
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__'
यहाँ मेरी कोड है
मुझे यह त्रुटि क्यों मिल रही है, और मैं इसे ठीक करने के लिए क्या कर सकता हूं?
ब्रैकेट काम नहीं करेंगे। 'Get()' फ़ंक्शन का उपयोग करें। 'configOptions.host = conifg.get ('सर्वर', 'होस्ट')' http://docs.python.org/2/library/configparser.html#examples – M456
ठीक है, आप उपयोग करने का प्रयास कर रहे हैं 'config' जैसे कि यह एक शब्दकोश था, और ऐसा नहीं है, यह एक' ConfigParser' उदाहरण है ... – kindall
भविष्य में, आप ['ConfigParser' दस्तावेज़] (http://docs.python.org) को संदर्भित करना चाहेंगे /2/library/configparser.html#configparser-objects)। –