简体   繁体   English

Pythons ConfigParser读取我的文件作为列表

[英]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. 我正在使用pygame创建游戏,并且正在使用ConfigParser解析地图图块的各种内容。 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. 我猜parse.read(filename)返回一个列表,而不是其预期的对象。 Here is my code I suppose. 我想这是我的代码。 I've been searching google but couldn't find anything related to this. 我一直在搜索google,但找不到与此相关的任何内容。

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. 我确定level1.ini文件是在同一文件夹中main.py文件。 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'] . ConfigParser.read返回成功解析的文件名列表,因此在您的示例中, self.config是文件名列表-可能是['level1.ini'] After parsing, you probably want to .get from the parser. 解析后,你可能想.get从解析器。 Something similar to this: 类似于以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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