How to save a binary file?

hidura at gmail.com hidura at gmail.com
Sun Oct 10 06:46:24 CEST 2010


Finally i had problems to save the files what are encoded i can't encode  
the string to save the file, any ideas?

On Oct 6, 2010 3:15pm, hidura at gmail.com wrote:
> Ppl thanyou, for all your help finally i did it!!!!! thanks, another  
> thing to who i can send a propose code, i fixed the little problem of the  
> wsig.input in Python 3 i will tested in the next months but i want to  
> share the code with the community, how i can do that?

> On Oct 6, 2010 3:13pm, hidura at gmail.com wrote:
> > Ppl thanyou, for all your help finally i did it!!!!! thanks, another  
> thing to i have to send a propouse code, i can fixed the litle problem of  
> the wsig.input in Python 3 i will tested in the next months but i want to  
> share the code with the comunnity, how i can do that?
> >
> > On Oct 6, 2010 1:45pm, MRAB python at mrabarnett.plus.com> wrote:
> > > On 06/10/2010 15:25, hidura at gmail.com wrote:
> > >
> > >
> > > When you put the 'wb' extension you have to pass a Encode the string
> > >
> > > Python does not accept a string on a wb file, Python3
> > >
> > >
> > >
> > >
> > > [snip]
> > >
> > > You are using Python 3 and type(str) returns ""?
> > >
> > >
> > >
> > > Binary data in Python 3 should be an instance of the 'bytes' class,  
> not
> > >
> > > an instance of the 'str' class.
> > >
> > >
> > >
> > > If you can't fix that, you could turn the string into bytes using:
> > >
> > >
> > >
> > > data = bytes(ord(c) for c in str)
> > >
> > >
> > >
> > > or by carefully choosing an encoding which would give the same result:
> > >
> > >
> > >
> > > data = str.encode('latin-1')
> > >
> > >
> > >
> > > Then you can save it:
> > >
> > >
> > >
> > > s = open('/home/hidura/test.jpeg', 'wb')
> > >
> > > s.write(data)
> > >
> > > s.close()
> > >
> > >
> > >
> > > I asked you to look at the result of repr so that you could see more
> > >
> > > clearly what the data actually looked like, an instance of str or an
> > >
> > > instance of bytes.
> > >
> > > --
> > >
> > > http://mail.python.org/mailman/listinfo/python-list
> > >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101010/d94df2ef/attachment.html>


More information about the Python-list mailing list