Binary data handling ?

Peter Hansen peter at engcorp.com
Thu Aug 28 13:52:53 EDT 2003


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





More information about the Python-list mailing list