简体   繁体   中英

Python remove everything after a space with hex \x00

I have a variable string with unknown length that has the important string at the left side and the unimportant things on the right side separated by a single space. How do I remove the unimportant information to the right?

I have tried rstrip, and split with no success.

Edit : I'll place the actual value that needs to be fixed.

"NPC_tester_contact() ) ntact()                                                                                                                                                                                                                 "

The very first space (the one left to the closed parenthesis) should have everything after including itself be marked as unimportant.

Edit : The output should be " NPC_tester_contact() "!

Look carefully at my string that I placed above. There is alot of whitespace after it as well. I assume that is what is causing the hiccup.

I have tried most of the solutions here and they either don't do anything or just produce whitespace.

repr(s) gives me.

'NPC_me_lvup_event_contact()\x00t()\x00act()\x00act()\x00ntact()\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\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\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\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
\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\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\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\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

It should be " NPC_me_lvup_event_contact() ".

Thanks!

Perhaps this is a better question. Is there a way to remove all characters after the first \\x00 hex that shows up in the string?

For some reason, it works sometimes and doesn't always work. The above example was done with the method that Levon posted.

Solution: Problem solved. This is more of a null byte rather than a space byte. The solution would of been any of the below using "\\x00" as the identifier instead of " ".

Thank you everyone!

UPDATE based on new string data:

Assuming s contains your string:

s.split('\x00')[0]

yields

'NPC_me_lvup_event_contact()'

split() will give you a list of strings separated by the character you specify with split . If none is specified space is used, in this case we use the hex character you are interested in.

USE split(' ')[0]

 >>> a = 'aaa bbb'
    >>> a.split(' ')[0]
    'aaa'
    >>> >
>>> mystring = 'important useless'
>>> mystring[:mystring.find(' ')]
'important'

split() w/o delimiter splits by any whitespace:

>>> "asdasd         xyz".split()[0]
'asdasd'

Use the split() function, and get the first item that it returns:

raw_string = 'NPC_tester_contact() ) ntact()  '
important = raw_string.split(' ')[0]

Will return:

NPC_tester_contact()
str = "important unimportant"
important = str.split(' ')[0]

try this:

lhs,rhs=s.split()  #lhs is what you want.

This only works if there is really only one space.

Otherwise, you can get lhs by (but you lose rhs):

lhs=s.split()[0]

try this, will assume that your string is stored in str
print str[0:str.index(" ")]

comment if it dont work, will solve it..

here is

My code
str = "NPC_tester_contact() ) ntact()                                                                                                                                                                                                                 "
print str[0:str.index(" ")] output NPC_tester_contact() link http://ideone.com/i9haI

and if you want output to be have surrounded with double-quotes then `print '"',str[0:str.index(" ")],'"'

you could use a regex type solution also. Something like:

import re

input_string = 'NPC_me_lvup_event_contact()\x00t()\x00act()\x00act()\x00ntact()\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\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\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\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\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\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\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\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

string_pat = re.compile(r'[a-zA-Z0-9\(\)_]+')
try:
    first_part = string_pat.findall(input_string)[0]
except IndexError:
    # There is nothing of interest for you in this string
    first_part = ''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM