472,342 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

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 7408
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David List | last post by:
I am wondering what I miss to be able to handle binary data from the mysql client. I have ensured that the user has file_priv set to 'Y' and that...
8
by: Bruce Lee | last post by:
Hi I have the following 2 questions: 1.How to write binary data to a text file in C? I have a number like 10001010110001 say, I need to put it in...
2
by: Feng | last post by:
Hi, I need a VB.Net function that reads in a stream of binary data coming in from a legacy data source. The data are actually hex numbers in...
4
by: Holger Marzen | last post by:
Hi all, AFAIK it is possible for columns to be very large, up to about 2 GB. Are there any hints or experiences about storing binary data...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We...
0
by: Wescotte | last post by:
I'm abit confused on how to work with binary data with an ODBC connection (My database is DB2 btw) Say I have a table like CREATE TABLE...
3
by: stockblaster | last post by:
Hello all.. Is it possible to convert a DataTable (i create the DataTable from a CSV file) into binary data and save it into an sql 2005 table...
0
by: perlprod | last post by:
Hi, I want to generate a data file that is to be read by a 'C' program. But I got in dilemma after checking the file generated by the following...
9
by: lokeshrajoria | last post by:
hello everybody, i need some help in binary file handling in perl. can anybody give me some information about binary file. actully i am reading data...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.