Python configparser Simple question...

Asked 2 years ago, Updated 2 years ago, 63 views

I'm using it in Python 3.7, but I want to use the config information in other class functions, but I keep getting the following error 네요ㅠ

import xml.etree.ElementTree as ET
import configparser

class Testconfig() :
    def __init__(self) :
        self.config = configparser.ConfigParser()
        self.config.read('config.ini')

class Testxml :
    def __init__(self, config) :
        self.config = config
        print(self.config)

    def xml(self) :
        tree = ET.parse(self.config['system']['directory']+'\\dmn.xml')
        root = tree.getroot()
        print(root)
testconfig = Testconfig()
testxml = Testxml(config)
testxml.xml()
['config.ini']
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-62-0ca9cdb0c8a9> in <module>
      1 testconfig = Testconfig()
      2 testxml = Testxml(config)
----> 3 testxml.xml()

<ipython-input-61-d14c520f9dac> in xml(self)
     14 
     15     def xml(self) :
---> 16         tree = ET.parse(self.config['system']['directory']+'\\dmn.xml')
     17         root = tree.getroot()
     18         print(root)

TypeError: list indices must be integers or slices, not str

python config parser xml

2022-09-20 10:19

1 Answers

The code uses dict, but it actually contains a list.


2022-09-20 10:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.