繁体   English   中英

Python使用十六进制\\ x00删除空格后的所有内容

[英]Python remove everything after a space with hex \x00

我有一个长度未知的可变字符串,其左侧有重要字符串,右侧有不重要的内容,中间用一个空格隔开。 如何在右侧删除不重要的信息?

我已经尝试过rstrip,但拆分失败。

编辑 :我将放置需要固定的实际值。

"NPC_tester_contact() ) ntact()                                                                                                                                                                                                                 "

第一个空格(左括号后面的空格)应将包括自身在内的所有内容标记为不重要。

编辑 :输出应为“ NPC_tester_contact() ”!

仔细看看我放在上面的弦。 后面还有很多空格。 我认为那是造成打ic的原因。

我在这里尝试了大多数解决方案,它们要么什么都不做,要么只是产生空白。

repr(s)给了我。

'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'

它应该是“ NPC_me_lvup_event_contact() ”。

谢谢!

也许这是一个更好的问题。 有没有办法删除字符串中第一个\\ x00十六进制之后的所有字符?

由于某些原因,它有时会起作用,但并不总是起作用。 上面的示例使用Levon发布的方法完成。

解决方案:问题已解决。 这更多的是空字节而不是空格字节。 解决方案可以是以下任何一种,使用“ \\ x00”作为标识符而不是“”。

谢谢大家!

UPDATE基于新的字符串数据:

假设s包含您的字符串:

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

产量

'NPC_me_lvup_event_contact()'

split()将为您提供一个字符串列表,该字符串列表由您使用split指定的字符split 如果未指定任何空格,则在这种情况下,我们使用您感兴趣的十六进制字符。

USE split('')[0]

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

split()不带分隔符,可按任何空格分割:

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

使用split()函数,并获取它返回的第一项:

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

将返回:

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

尝试这个:

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

仅在实际上只有一个空间的情况下才有效。

否则,您可以得到lhs (但会丢失rhs):

lhs=s.split()[0]

尝试此操作, 将假定您的字符串存储在str
print str[0:str.index(" ")]

评论,如果它不起作用,将解决它。

这是

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

并且如果您希望输出用双引号引起来,则`print'“',str [0:str.index(”“)] ,,”“

您也可以使用正则表达式类型的解决方案。 就像是:

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 = ''

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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