Python Question
I need to save a list into a tab-deliminated text-file. (Mark’s RSSParser sends me the last-modified of the feed as a list which I need to save to the config file so I can recall it when I next run and feed it to the site to make sure I’m not grabbing a duplicate).
The logical way to do this is to serialize the list and save it. For this, I find Pickle which does exactly that. The problem is that the format it saves out to contains line breaks, unideal in a tab-deliminated file.
The question is how do I save (or indeed print anywhere) a string as a raw string? The tutorial says that to output a raw string I prefix it with r, which causes logical problems when I’m trying to output from a variable.
Extra bonus points if you can tell me where I should have found this information.
- 2003-05-08 18:49:19
- Updated 6 weeks later
- By Aquarion
- More Journal Entries
- Filed under Aquaintances & Python
Simon Callan:
This sounds daft.
fh.write “%st%sn” % current)If you want to save a list to a text file, you don’t want pickle, as it uses an undocumented internal format.
I would go something like
data = ( “1.1”, “1.2”), (“2.1”, “2.2”), (“3.1”, “3.2”))
fh = file “name”, “wt”)
for current in data:
fh.close()
which would produce a file like
1.1t1.2
2.1t2.2
3.1t3.2
Also, AFAIK, raw strings only apply to strings in the source of a python program, where it prevents the compiler from doing things to escape characters. Ie. “t” is a single character string with a tab in it, while r”t” is a 2 character string consisting of a backslash, followed by a t character.
All printing is raw, unless you get into things like unicode, etc.