[Tutor] Best way to strip string string padded with nulls

Patrick K. O'Brien pobrien@orbtech.com
Wed, 6 Jun 2001 13:45:19 -0500


Here's one problem with my solution.

null = '\x00'
c = conversation.Request("Contact1->Company")
c = c[:c.index(null)]

If there is no null in c, index returns a ValueError that would have to be
dealt with. Worse is if I used c.find(null) since find would return -1 which
would eat the last character of c. Looks like I still need to work on this.

---
Patrick K. O'Brien
Orbtech
"I am, therefore I think."

-----Original Message-----
From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
Patrick K. O'Brien
Sent: Wednesday, June 06, 2001 12:00 PM
To: Python Tutor
Subject: [Tutor] Best way to strip string string padded with nulls

I'm working with DDE to get data out of Goldmine. The data returned from
Goldmine is padded with nulls (and other random data, see example below). I
need to strip the resulting string down to just the relevant data. I have
some example code here that works, but I'm looking for suggestions for a
simpler or more elegant approach. Thanks.

Here is a snippet from an interactive session so you can see what I have
described above:

>>> c = conversation.Request("Contact1->Company")
>>> c
'GoldMine Software
Corporation\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Ok\xf8\xff'
>>> i = c.index('\x00')
>>> i
29
>>> a = c[:i]
>>> a
'GoldMine Software Corporation'

Of course, this could also be collapsed to the following:

c = c[:c.index('\00')]

But considering the fact that I'll have to do this to every string that gets
returned via DDE requests, I'm hoping there is a better way. Thanks for the
help.

---
Patrick K. O'Brien
Orbtech
"I am, therefore I think."



_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor