Binary data handling ?

Peter Hansen peter at engcorp.com
Thu Aug 28 11:10:58 EDT 2003


Bill Loren wrote:
> 
> Hello ppl,
> 
> I'm having difficulties to accomplish some simple chores with binary data.
> I'm having a string (?) which I received via an HTTP transactions which is a
> binary file.
> Problem is the webserver I'm communicating with injected a \x0D before every
> \x0A,
> and I need to remove those 0x0D characters from my buffer before saving it
> to disk.

This sounds wrong.  I don't think a properly configured web server should 
be transmitting unencoded binary files with newline conversion.

> any ideas ?
> 
> I tried the following without any success:
> string.replace("%c%c" % (13,10), "%c" % (10))
> string.replace("\x0d\x0a", "\0x0a")

Strings are immutable.  Are you expecting the above to change the string
(which doesn't happen) or to return a new string with the changes made?
Assign the result of the replace() call to a new variable and it should
work.  (Except in the latter example you should have \x0a, not \0x0a.)

-Peter




More information about the Python-list mailing list