简体   繁体   中英

Pythons ConfigParser reading my file as a list

I'm creating a game with pygame and I'm using ConfigParser to parse various things for map tiles. But when I get to the part where I do

parse.read(filename)

It outputs this error

self.level = self.config.get("level","map")
    AttributeError: 'list' object has no attribute 'get'

I'm guessing parse.read(filename) returned a list instead of its intended object. Here is my code I suppose. I've been searching google but couldn't find anything related to this.

import pygame
import ConfigParser

parse = ConfigParser.ConfigParser()

class MakeLevel():
    def MapMake(self,spriteList,filename):
        self.config = parse.read(filename)
        self.level = self.config.get("level","map")
        self.LegendDict = self.config.get("dictionary")
        self.Proper = []
        self.newTile = None
        self.x = 0
        self.y += 50
        #Get propper legend stats
        for items in LegendDict:
            for row in level:
                for col in row:
                    if col == items:
                        #LegendDict[items]
                        self.image = self.config.get(items, "image")
                        self.newTile =  MapTile(self.image,self.x,self.y)
                        return spriteList.add(self.newTile)
                x += 50
            y += 50
            x = 0


class MapTile(pygame.sprite.Sprite):
    def __init__(self,image,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image)
        self.rect = Rect(x, y, 32, 32)


class Controller():
    def __init__(self):
        pass

    def Keys(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    return 'a'
                if event.key == pygame.K_d:
                    return 'd'

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a:
                    return 'a up'
                if event.key == pygame.K_d:
                    return 'd up'

AllSprites = pygame.sprite.Group()

makeLevel = MakeLevel()

makeLevel.MapMake(AllSprites,"level1.ini")

AllSprites.draw()

I also tried opening the file beforehand and it still did not work.

mapFile = open("level1.ini")
makeLevel.MapMake(AllSprites, mapFile)

I made sure the level1.ini file is in the same folder as the main.py file. Hopefully the problem isn't something so obvious.

ConfigParser.read returns a list of filenames successfully parsed, so in your example, self.config is a list of filenames -- likely ['level1.ini'] . After parsing, you probably want to .get from the parser. Something similar to this:

    def MapMake(self,spriteList,filename):
        parse.read(filename)
        self.level = parse.get("level", "map")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM