Writing Formatted txt to a file (xtreme newbie q)

Fuzzyman michael at foord.net
Tue May 11 07:30:34 EDT 2004


phrogeeb at hotmail.com (Uri) wrote in message news:<8a0bb987.0405101503.2656788a at posting.google.com>...
> I am working on a program that reads (through Tkinter and a form of
> text boxes, checkboxes, radio switches, sliders, etc) a set of values,
> saves them to a file, and is also capable of loading from the file so
> that the sliders switches checkboxes etc will have the paramaters
> defined in the text file (that last part is peripheral, eventual, but
> if there's something i have to do to set that up immediately I'd like
> to do that.)
> 
> Right now, I'm basicly doing this in simple question-answer text
> no-GUI format.
> 
> I've managed to set up the q&a scenario, and using a print statement
> set it up like I'd like it to go into the file (space-dileneated)
> 
> However, I'm lost as far as writing it to the file in a formatted
> form. I've looked around, and I'm pretty sure I can rule out pickle as
> not what i want, but I'm having trouble figuring out what i do want.
> The data is in the format of a list right now (maybe a dictionary at
> some point) and there is one question and one answer per column. I
> would like to set it up as "Question: Answer" in side-by-side
> formatted columns, or perhaps "Question" over "Answer" in two rows,
> answers on bottom.
> 
> Thanks in advance!
> 
> (Also, is there a pretty popular web-based forum for python or no?)


I'd like to take this opportunity to reccomend ConfigObj.
It is for very simply reading and writing config files like this.

It is available from :
http://www.voidspace.org.uk/atlantibots/pythonutils.html#configobj

It works with keywords and values. 
In general creating a config file is as easy as :

from configobj import ConfigObj
configobj = ConfigObj(filename)
configobj['keyword1'] = value1
configobj['keyword2'] = value2
.
.
.
configobj.write()

Which creates a new config file. It can then be read in again with :
configobj = ConfigObj(filename)
value1 = configobj['keyword1']
value2 = configobj['keyword2']
.
.

Values can be either strings or lists of values.
e.g. configobj['keyword1'] = [value1, value2, value3]

The text files are very straightforward and can be edited by hand -
comments on the same line as a keyword/value will be preserved.

They look like :

'keyword1' : 'value1'
'keyword2' : 'value2'
.
.
.

Keywords are case-insensitive. Quoting is optional....
There are lot's more options and ways to use ConfigObj - but that's
the essence of the matter.... - it behaves like a dictionary, you pass
a filename in to read it and use the write method to write it back out
again.

In actual fact ConfigObj is just undergoing a major revamp (again) -
although the current version works fine.
The version 3 series is *nearly* ready - and will include support for
config files with section markers..... as well as a few other nifty
features and changes. Unfortunately a few of the changes are
incompatible ones - but it doesn't affect the basic features.

(The most notable one is that the update method has been changed to
writein - because update clashes with a dictionary method name which I
want to make available).

If you need any help I would be only to happy to help - best address
to get me at is :
mike AT pcblokes DOT com

Regards,

Fuzzy

Full current changelist for Version 3.0.0 - imminent

CHANGELOG

IMMENENT       Version 3.0.0
Several incompatible changes - another major overhaul and change.
Lot's of improvements though.
Added support for standard config files with sections. This has an
entirely new interface - each section is a dictionary of values.
Changed the update method to be called writein - update clashes with a
dict method.
Made configspec, force_return and newline keyword arguments.
Removed support for adding dictionaries - use update instead.
Now subclasses a new class called caselessDict. This should add
various dictionary methods that could have caused errors before.
It also preserves the original casing of keywords when writing them
back out.
Comments are also saved using a caselessDict.
Using a non-string key will now now raise a TypeError rather than
converting the key.
Added the 'isflatfile' function.
Added an exceptions keyword for *much* better handling of errors.
Made creatempty=False the default.
Now checks indict *and* any keyword args. Keyword args take precedence
over indict.
Added a new keyword argument called default.
' ', ':', '=', ',' and '\t' are now all valid dividers where the
keyword is unquoted.
ConfigObj now does no checking against configspec when you set items
yourself - it only checks when you parse the file.
delete and add methods removed (they were unnecessary).
Docs rewritten - to include all this gumph and more..... Actually
ConfigObj is *really* easy to use.



More information about the Python-list mailing list