Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:44:14+00:00 2026-06-17T12:44:14+00:00

A little bit about the code; what I’m trying to do is turning a

  • 0

A little bit about the code; what I’m trying to do is turning a section into a Dict so I can easily manage it.
Of course, because ConfigParser returns everything as a string (most of the time anyway), I have to change it into the desired type. And that’s where my problem starts.

Code

import ConfigParser, pygame

parser = ConfigParser.SafeConfigParser()

class loadfile(object):

    def __init__(self, filename):
        self.filename = filename

    def load(self):
        parser.read(self.filename)

        for section_name in parser.sections():
            vars()[section_name] = {}

            for name, value in parser.items(section_name):
                if value.isdigit():
                    value = int(value)
                elif value == "None":
                    value = None
                elif value == "True" or value == "False":
                    value = parser.getboolean( section_name, name )
                else:
                    value = vars()[ parser.get( section_name, name ) ]

                vars()[section_name].update( { name : value } )

            print vars()[section_name]

    def save(self):
        pass

loadfile("config.ini").load()

config.ini

[Display]
Width       : 800
Height      : 600
Depth       : 32
Caption     : 45
Flags       : pygame.RESIZABLE
Icon        : None
Mouse       : True
FPS         : 30

; Key configuration;

[Keys]
Left        : pygame/K_LEFT
Right       : pygame.K_RIGHT
Jump        : pygame.K_UP
Duck        : pygame.K_DOWN
Sprint      : pygame.K_RSHIFT
Attack_1    : pygame.K_a
Attack_2    : pygame.K_s
Attack_3    : pygame.K_d
gameMenu    : pygame.K_ESCAPE
Dialogue    : pygame.K_RETURN

Error

Traceback (most recent call last):
  File "C:\Users\***\Desktop\config2.py", line 35, in <module>
    loadfile("config.ini").load()
  File "C:\Users\***\Desktop\config2.py", line 27, in load
    value = vars()[ parser.get( section_name, name ) ]
KeyError: 'pygame.RESIZABLE'

The same error occurred before I put the code in it’s own class, if I used import pygame but it mysteriously went away when I used from pygame import *

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:44:15+00:00Added an answer on June 17, 2026 at 12:44 pm

    Here’s some code that I have written for this, it doesn’t use ConfigParser but should work.
    (atleast in your case)

    class loadfile(object):
    
        def __init__(self, filename):
            self.filename = filename
    
        def load(self):
            x = open(self.filename).read()     # open as file and read
            d,k = x.split('; Key configuration;')   # split at Key configration
            d = d.splitlines()
            k = k.splitlines()
            ddict = {}
            for i in d:
                    i = i.split(':')
                    i = [z.strip() for z in i] # strip whitespace(s)
                    if len(i) == 2: # a valid assignment line
                            ddict[i[0]] = i[1] # assignment of value
            #same for keys
            kdict = {}
            for i in k:
                    i = i.split(':')
                    i = [z.strip() for z in i]
                    if len(i) == 2:
                            kdict[i[0]] = i[1]
            print '---Display---'
            for key in kdict:
                print '%10s:%19s' % (key,kdict[key]) # print display part
            print '---Keys---'
            for key in ddict:
                print '%10s:%19s' % (key,ddict[key]) # print key part
            self.kdict = kdict
            self.ddict = ddict
        def save(self):
            pass
    
    x = loadfile("config.ini")
    x.load()
    

    Output (from your config.ini)

    ---Display---
      Attack_1:         pygame.K_a
      Dialogue:    pygame.K_RETURN
         Right:     pygame.K_RIGHT
          Jump:        pygame.K_UP
      Attack_3:         pygame.K_d
      Attack_2:         pygame.K_s
          Duck:      pygame.K_DOWN
        Sprint:    pygame.K_RSHIFT
      gameMenu:    pygame.K_ESCAPE
          Left:      pygame/K_LEFT
    ---Keys---
       Caption:                 45
        Height:                600
         Width:                800
         Depth:                 32
         Flags:   pygame.RESIZABLE
           FPS:                 30
         Mouse:               True
          Icon:               None
    

    Also, if you use the following type of format for ini file(format may not be the right word), it might be easier to work with.

    [Display]
    Width=800
    Height=600
    Depth=32
    Caption=45
    Flags=pygame.RESIZABLE
    Icon=None
    Mouse=True
    FPS=30
    -----
    [Keys]
    Left=pygame/K_LEFT
    Right=pygame.K_RIGHT
    Jump=pygame.K_UP
    Duck=pygame.K_DOWN
    Sprint=pygame.K_RSHIFT
    Attack_1=pygame.K_a
    Attack_2=pygame.K_s
    Attack_3=pygame.K_d
    gameMenu=pygame.K_ESCAPE
    Dialogue=pygame.K_RETURN
    -----
    

    This can be executed(after spliting from ‘—–‘, and removing first line of each of these.([keys],[display])

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find out a little bit more about how the preprocessor works
I'm learning everyday a little bit more about android developing and json code. But
I'm reading a little bit about the eBay API, but I can't find anything
I just read little bit about macro and was trying to create the one
I am a little bit confused about the fact that we can just catch
I know a little bit about TextWatcher but that fires on every character you
I only read a little bit about this topic, but it seems that the
I'm talking about both server-side and client-side. I know a little bit about Go,
I'm doing a navigation based application. Let me explain a little bit about the
I am little bit confuse about log4j in grails. I need to log info

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.