469,299 Members | 2,050 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

Binary data handling ?

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.

any ideas ?

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

thx
~B
Jul 18 '05 #1
5 7175
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
Jul 18 '05 #2
"Bill Loren" <lo*****@bezeqint.net> wrote in message news:<ma**********************************@python. org>...
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.

any ideas ?

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

thx
~B

# A short function to generate a string with 0x0D's
def make_bin(): .... bytes=[]
.... for i in range(20):
.... bytes.append(chr(i))
.... # every 5th char is a 0x0D
.... if not i%5:
.... bytes.append(chr(0x0D))
.... return ''.join(bytes)
.... binary=make_bin()
binary '\x00\r\x01\x02\x03\x04\x05\r\x06\x07\x08\t\n\r\x0 b\x0c\r\x0e\x0f\r\x10\x11\x12\x13' # split the string at the 0x0D's and join it again
binary_without_x0D=''.join(binary.split(chr(0x0D)) )
#et voila
binary_without_x0D '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\ x0e\x0f\x10\x11\x12\x13'


Regards
Peter
Jul 18 '05 #3
Bill Loren wrote:

about the code problem, I did fix that bug you mentioned, but it still
doesn't work.
the code is:
data = data.replace(...the two options I mentioned before...)
but alas... no replacement...


Since the following clearly works, you must be confused about what
is in the string called "data" prior to the replacement:

C:\>python22
Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
s = 'test 1\r\ntest 2\rtest 3\ntest4\r\n\r\ntest5'
s 'test 1\r\ntest 2\rtest 3\ntest4\r\n\r\ntest5' t = s.replace('\r\n', '\n')
u = s.replace('\x0d\x0a', '\x0a')
v = s.replace('%c%c' % (13, 10), '%c' % (10))
t 'test 1\ntest 2\rtest 3\ntest4\n\ntest5' u 'test 1\ntest 2\rtest 3\ntest4\n\ntest5' v

'test 1\ntest 2\rtest 3\ntest4\n\ntest5'

(Please post to the newsgroup/mailing list instead of mailing directly,
so that others can benefit from or participate in the discussion.)

-Peter

Jul 18 '05 #4
"Bill Loren" <lo*****@bezeqint.net> wrote:

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.

any ideas ?


I'll bet you real money that the problem is not in the web server. I'd
wager that the string is correct when you receive it, but that you are
writing it to file like this:
file('out.txt','w').write(download)

On a Windows system, that'll turn all the LFs into CR-LFs. Use this
instead:
file('out.txt','wb').write(download)
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #5
Indeed !

thanks !!!

if I use the 'wb' will it work on a unix system, too ?

~B
----- Original Message -----
From: "Tim Roberts" <ti**@probo.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Saturday, August 30, 2003 7:11 AM
Subject: Re: Binary data handling ?

"Bill Loren" <lo*****@bezeqint.net> wrote:

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 abinary 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 itto disk.

any ideas ?


I'll bet you real money that the problem is not in the web server. I'd
wager that the string is correct when you receive it, but that you are
writing it to file like this:
file('out.txt','w').write(download)

On a Windows system, that'll turn all the LFs into CR-LFs. Use this
instead:
file('out.txt','wb').write(download)
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by David List | last post: by
8 posts views Thread by Bruce Lee | last post: by
2 posts views Thread by Feng | last post: by
4 posts views Thread by Holger Marzen | last post: by
6 posts views Thread by | last post: by
reply views Thread by Wescotte | last post: by
3 posts views Thread by stockblaster | last post: by
reply views Thread by perlprod | last post: by
9 posts views Thread by lokeshrajoria | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.