An pulling this into a dictionary within a running app by opening the config file and reading each line and then splitting the line at = and putting the 'files' part as the key and the rest as the value. This looks something like:

This took me a while to get working so now I'm not sure if the way I got it working is really the best way. But I was un-successful at doing it any other way. Ultimately I would like the outcome to be something like:

It won't give you a real python list, but you can store json lists in the config file, and just use json.loads() to get a python list.Alternatively, you could just use json for config.

Perfect a lib to do config parsing-> I'll check it out! Hmm json... This might be a direction that I need to take that I have been avoiding. Every time I start down the json path it seems like a tital wave of info and I just stop... Got any good tutorials to get one going?

hansn wrote:Just a word of advice: don't worry about the 'right' way, just worry about making it work the way you want it to.

What's wrong with it? Use str.strip("'") to remove the extra single quotes. (That's str.strip(" ' ") with a single quote in the middle)

Well that's probably true! Sometimes when I wrestle to get a solution it feels like there just has got to be a better way. This one definitely left me feeling kind of dirty. Hmm... I think I was so focused on this that I was in a mental block. I'll try it with the 'strip("'")' thing!

JSON is nice because you can often dump whatever Python object you want to a file, and get it back very easily as a Python object (dictionary, list, string). I use JSON often and love it. I feel like it's straightforward.

Perhaps you would prefer config files, since while JSON is considered "human readable" it looks like the config files have less noise and are probably easier for regular users. You can save JSON with nice indentation and such instead of the regular blob form by checking out the option arguments to dump() (or dumps()). Anyway, I hope you can make an informed choice now! Feel free to followup with any questions.

Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.

Beats me: why do you want it like this? Config file is a file that can be easily changed in a text editor. In most cases, you should assume that a user will edit it and create some mess. And it's much easier to mess up what you like (?) than the following:

micseydel wrote:Perhaps you would prefer config files, since while JSON is considered "human readable" it looks like the config files have less noise and are probably easier for regular users. You can save JSON with nice indentation and such instead of the regular blob form by checking out the option arguments to dump() (or dumps()). Anyway, I hope you can make an informed choice now! Feel free to followup with any questions.

I think I'd prefer a config file. After messing around with json for a bit (dumping and loading) I realized the output isn't what I'd want in a config file. However, I did see how I can use it for state saving and things like that which I'll definitely do.

I messed around a bunch with ConfigParser and agree this is the way to go. It doesn't recognize lists but it allows them as a string (both reading and writing). I'm probably silly for wanting lists but I love lists and dictionaries... What can I say? The main reason for wanting a list here is that ease in using an item (which is a directory) that may or may not have white space in it and so as to not have the config file editor having to do escaping.

So I went with ConfigParser and this changed things a bit. Ultimately the point here was to make the config file fail nicely with anything the user threw at it. So I also wanted to be able to handle an incoming list from the config file. ConfigParser doesn't parse list values, so I have to grab and deal with that myself. I do this with something like:

files=[x.strip('\'') for x in config.get('Main',var).strip('[]').split(',') if len(x)>0]

This list comprehension is a bit different from above as I found that it didn't play with with a config file option of 'files=[]'. But now it does play well with it or any other blank files option value.

I doubt strip("[]") is doing as you expect. Also, you probably want to split on ", " rather than "," (note the space) if your list is as you expect, since when you try to strip off the quote character it won't work if there's a space on the end. Also note that if you're creating the file by using str() on the list, that a string inside the list will have a double quote (") if there is a single quote in that string so you won't be stripping the right thing. And if you read that string, then write it again, eventually you can end up with insanity like this

What you're doing is called "rolling your own" and I recommend against it. It won't be robust. You can achieve something much like what you want by making use of the csv module. You don't have to save your whole config file as a csv, you can simply use the module as a robust way of creating the list as a string and retrieving it, but still store that string in your config file.

Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.

micseydel wrote:I doubt strip("[]") is doing as you expect. Also, you probably want to split on ", " rather than "," (note the space) if your list is as you expect, since when you try to strip off the quote character it won't work if there's a space on the end. Also note that if you're creating the file by using str() on the list, that a string inside the list will have a double quote (") if there is a single quote in that string so you won't be stripping the right thing. And if you read that string, then write it again, eventually you can end up with insanity like this

What you're doing is called "rolling your own" and I recommend against it. It won't be robust. You can achieve something much like what you want by making use of the csv module. You don't have to save your whole config file as a csv, you can simply use the module as a robust way of creating the list as a string and retrieving it, but still store that string in your config file.

Yes I did find that out and I was checking back in to update things. I decided to remove the (or any) space after the comma with (basically this...):

RYO Yeah thats funny! I checked out the csv module and this seems like a bit over kill for what I'm trying to do. I do like that the module exists though and I may have a need for it shortly to do exactly the csv export import kind of thing! Thanks

Last edited by pnelsonsr on Fri Jan 31, 2014 4:07 pm, edited 1 time in total.