SuperDummy
SuperDummy

Reputation: 51

How To Ident A List Of Values To The Right In Config File Python

So I need to be able to read a list of values that a user puts in a config option. I noticed however, in order for the Configparser to read more than one value, it needs to be indented. I was wondering is there a way for me to read more than one value without having to do an indentation? Or do the indentation pragmatically?

Ideally I'd like the user to not have to do any kind of indentation on their end. Is there another file type I can use where I can avoid having to do that kind of indentation?

config.ini
[log parameters]

# This is how the values for my "apps" option is set
apps:
google 
amazon
facebook
twitter

apps:
 google #-> Indent these automatically so the user doesn't have to do it. 
 amazon
 facebook
 twitter

Upvotes: 0

Views: 190

Answers (1)

user17328377
user17328377

Reputation:

You can use a for loop to read every line in config.ini and write them in to an another file with indenting strings.

config.ini

apps:
google 
amazon
facebook
twitter

code

x = open("config.ini", "r")
f = open("output.ini", "a")

for i in x:
    if ":" in i:
        # write titles without indenting
        f.write(i)
        pass
    else:
        # include spaces you need to indent
        f.write("  ",+i)
        pass

x.close()
f.close()

output.ini

apps:
  google 
  amazon
  facebook
  twitter

Upvotes: 1

Related Questions