[Tutor] How to write list to file

orbitz at ezabel.com orbitz at ezabel.com
Wed Jan 28 16:56:55 EST 2004


Why don't you simply use repr()?
Also, make sure your bytetotal is correct.

>>> sys.stdout.write(repr(['1', '2', '\t']))
['1', '2', '\t']

this way you can just use eval to make your list again.  Also why not
use pickle?


On Wed, 28 Jan 2004 12:58:38 -0500 (EST)
Vicki Stanfield <vicki at thepenguin.org> wrote:

> I have the following code:
> 
> print bytearray[0:bytetotal]
> outfile.write("[")
> for byte in bytearray[0:bytetotal]:
>     outfile.write(byte)
> outfile.write("]")
> 
> The print statement prints this:
> 
> ['F', '8', '\t']
> 
> but the outfile has only this:
> 
> [40	]
> 
> I think the problem is that the program is trying to write [40 46 30
> 38\t], but for some reason stops at the 40. Why would the different
> way of writing cause this behavior?
> 
> --vicki
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list