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

Remco Gerlich scarblac@pino.selwerd.nl
Wed, 6 Jun 2001 20:01:12 +0200


On  0, "Patrick K. O'Brien" <pobrien@orbtech.com> wrote:
> 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.

Hmm, seems I solved the wrong problem *again*. You want to delete the stuff
after the nulls as well. I think that last line of code you have there is
the best option.

-- 
Remco Gerlich