Write and read objects from files .

Gerhard Häring gh_pythonlist at gmx.de
Sat Oct 27 13:17:26 EDT 2001


On Sat, Oct 27, 2001 at 06:54:25PM +0200, Amit Weisman wrote:
> Hi 
> I'm a python beginner and looking for :
> 1. A way to save a list on a file (as a string) and then retrieve it
> back (read the string from the file and build automatically a list) . 

If you don't care for the format of the file, you can use the pickle
module to save a list to a file and read the list back. In fact, you can
use pickle to save (almost) everything to a file: lists, strings, ints,
objects, even Python modules and functions, ... The docs of the pickle
module also contains an example.

If the list contains strings, another possibility would be to open a
file, write all the list elements to the file (and a newline after each
call). This might best be done with a for loop over the list elements,
like:

list = ["this", "is a", "list"]
for item in list:
    ...

Reading back could be done similarly. I'd recommend to open a file and
use its "readlines()" method, which will give you a list already. You'll
also get a newline for each item in the list, but this can be stripped
easily.

> 2. Is there a class or function that provides mechanism to check an
> e-mail address ? 

You mean a way to verify if a string is a valid email-address? There is
no such function in the Python standard library. But I'm sure there are
several implementations out there. I'd start with thinking about the
requirements of an email address (only valid characters, an "@" sign
somewhere, after the "@" sign a domain name).

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id 86AB43C0
public key fingerprint: DEC1 1D02 5743 1159 CD20  A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))




More information about the Python-list mailing list