transform strings list in a int lists or other type???

Dennis Lee Bieber wlfraed at ix.netcom.com
Wed Sep 25 23:19:37 EDT 2002


jubafre at brturbo.com fed this fish to the penguins on Wednesday 25 
September 2002 04:29 pm:

        Haven't you asked a version of this /each/ day for the last few days?

>                   Can i tranform a list of strings in a list of int,
>                   or other type in python???
> and also write in a file? I think the open(path,'wb') in __builtin__
> dont´t write in a file other types, just strings, rigth???
> 

        ALL I/O in Python is effectively a stream of bytes. The "b" option on 
the open ONLY controls the interpretation of line-endings. IE, on 
Windows reading a file with "b" means you see both the <cr> and the 
<lf> character; without the "b" (normal mode) the <cr><lf> is converted 
to just a "newline" (<lf>) for internal use.

        To write a "binary" file requires you to either manipulate /each/ byte 
of your data, OR to use the struct module to pack your data into a 
string (full of unprintable characters) which can be written.

import struct
ots = struct.pack(">2L2H12s30pB", 0x10346f89, 563993, 0x0d0a, 32767,
        "Some Text", "More text with a count byte", ord("\r")

print ots

Lots of unprintables in that... Cut&Pasted from the terminal window:
4o›       ore text with a count byte

print repr(ots)

'\x104o\x89\x00\x08\x9b\x19\r\n\x7f\xffSome Text\x00\x00\x00\x1bMore 
text with a count byte\x00\x00\r'

        I used the ">" in the pack format string to ensure that the byte 
sequence for output is "big-end first" so the output representation is 
the same as the input numbers -- default on my machine is "little-end 
first".

Compare:
0x10346f89      to the output... You have to take into account that 
printable characters show up as single byte characters rather than an 
encoded \xnn set.

0x10   34   6f   89
\x10    4    o \x89

--
 > ============================================================== <
 >   wlfraed at ix.netcom.com  | Wulfraed  Dennis Lee Bieber  KD6MOG <
 >      wulfraed at dm.net     |       Bestiaria Support Staff       <
 > ============================================================== <
 >        Bestiaria Home Page: http://www.beastie.dm.net/         <
 >            Home Page: http://www.dm.net/~wulfraed/             <



More information about the Python-list mailing list