Writing Formatted txt to a file (xtreme newbie q)

U

Uri

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?)
 
S

Sean Berry

How this will actually be written is dependent on how you have your data
stored. If it is in a list is it like this

[[question1, answer1], [question2, answer2], etc...]
or
['question1=answer1', 'question2=answer2', etc...]
or some other way.

Once you can tell me that, I am sure I can give you a pretty easy way to
write it out to a formatted file.
 
U

Uri

[[question1, answer1], [question2, answer2], etc...]
or
['question1=answer1', 'question2=answer2', etc...]
or some other way.
It is stored in the format: ([question1, answer1]), made up of the
question list and the answer list zip()'ed together. Is there a better
way to do this? I had considered doing it with a dictionary, but i had
problems with this, and found the easiest way to be zipped two lists
together. WE can also keep them apart, I suppose.

I hadn't realized it depended so heavily on which way you do it. =)

Thanks!
 
F

Fuzzyman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,002
Messages
2,570,257
Members
46,857
Latest member
ArleenWill

Latest Threads

Top