yaml looks pretty interesting. Also, I wouldn't have to change much, I would still use the same function, and still output a dict.
Thanks! -Zach On Thu, Jul 2, 2009 at 11:55 PM, Javier Collado<javier.coll...@gmail.com> wrote: > Hello, > > Have you considered using something that is already developed? > > You could take a look at this presentation for an overview of what's > available: > http://us.pycon.org/2009/conference/schedule/event/5/ > > Anyway, let me explain that, since I "discovered" it, my favourite > format for configuration files is yaml (http://yaml.org/, > http://pyyaml.org/). It's easy to read, easy to write, available in > different programming languagues, etc. In addition to this, type > conversion is already in place so I think it covers your requirement. > For example: > > IIn [1]: import yaml > > In [2]: yaml.load("""name: person name > ...: age: 25 > ...: is_programmer: true""") > Out[2]: {'age': 25, 'is_programmer': True, 'name': 'person name'} > > Best regards, > Javier > > 2009/7/2 Zach Hobesh <hob...@gmail.com>: >> Hi all, >> >> I've written a function that reads a specifically formatted text file >> and spits out a dictionary. Here's an example: >> >> config.txt: >> >> Destination = C:/Destination >> Overwrite = True >> >> >> Here's my function that takes 1 argument (text file) >> >> the_file = open(textfile,'r') >> linelist = the_file.read().split('\n') >> the_file.close() >> configs = {} >> for line in linelist: >> try: >> key,value = line.split('=') >> key.strip() >> value.strip() >> key.lower() >> value.lower() >> configs[key] = value >> >> except ValueError: >> break >> >> so I call this on my config file, and then I can refer back to any >> config in my script like this: >> >> shutil.move(your_file,configs['destination']) >> >> which I like because it's very clear and readable. >> >> So this works great for simple text config files. Here's how I want >> to improve it: >> >> I want to be able to look at the value and determine what type it >> SHOULD be. Right now, configs['overwrite'] = 'true' (a string) when >> it might be more useful as a boolean. Is there a quick way to do >> this? I'd also like to able to read '1' as an in, '1.0' as a float, >> etc... >> >> I remember once I saw a script that took a string and tried int(), >> float() wrapped in a try except, but I was wondering if there was a >> more direct way. >> >> Thanks in advance, >> >> Zach >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > -- http://mail.python.org/mailman/listinfo/python-list