Aquarionics

Thursday 8th May 2003

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.

Those who spoke on this:

gravatar image

Simon Callan:

2003-05-09 08:35 15 hrs after the Original Article

This sounds daft.
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.write “%st%sn” % current)
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.

Comment Link


Nicholas 'Aquarion' Avenell is a web developer in London, you can find out more about him or how to get in touch.

There are more Articles, Projects, Journal Entries, Photographs and things that defy description here, too.

If you're looking for something specific, there are Calendar & Category -based lists of everything.

And if you want to follow stuff that appears here, try a Syndication Feed, or the generic Feed of everything.

Aquarionics on Livejournal


Aquarion [updating]
Twitter last updated


More Journal:

[RSS Icon]
[ESF Icon]
[CDF Icon]

That which is relevant:


Explain Ads
© 2000 to 2008 inclusive Nicholas Avenell
All comments are the property of their creators, published with permission
(Unless otherwise indicated, the opinions and sentiments expressed on this site are those of the author and not of any organisation of which he is an affiliate, including his employer. Caveat Lector, E&OE. sigh)
0.397 seconds, 16 queries, 2.65Mb on Fri, 07 Nov 2008 12:29:46 +0000
Generated by Epistula Version 2.0.3