简体   繁体   English

通过查找非零字符在python中拆分字符串

[英]String splitting in python by finding non-zero character

I want to do the following split: 我要进行以下拆分:

input: 0x0000007c9226fc output: 7c9226fc
input: 0x000000007c90e8ab output: 7c90e8ab
input: 0x000000007c9220fc output: 7c9220fc

I use the following line of code to do this but it does not work! 我使用下面的代码行来执行此操作,但是它不起作用!

split = element.rpartition('0')

I got these outputs which are wrong! 我得到了这些错误的输出!

input: 0x000000007c90e8ab output: e8ab
input: 0x000000007c9220fc output: fc

what is the fastest way to do this kind of split? 进行这种拆分的最快方法是什么? The only idea for me right now is to make a loop and perform checking but it is a little time consuming. 对我而言,目前唯一的想法是制作一个循环并执行检查,但这很耗时。

I should mention that the number of zeros in input is not fixed. 我应该提到输入中零的数目不是固定的。

Each string can be converted to an integer using int() with a base of 16. Then convert back to a string. 可以使用以16为底的int()将每个字符串转换为整数。然后转换回字符串。

for s in '0x000000007c9226fc', '0x000000007c90e8ab', '0x000000007c9220fc':
    print '%x' % int(s, 16)

Output 输出量

7c9226fc
7c90e8ab
7c9220fc
input[2:].lstrip('0')

That should do it. 那应该做。 The [2:] skips over the leading 0x (which I assume is always there), then the lstrip('0') removes all the zeros from the left side. [2:]跳过前导0x (我假设始终在该位置),然后lstrip('0')从左侧删除所有零。

In fact, we can use lstrip ability to remove more than one leading character to simplify: 实际上,我们可以使用lstrip功能删除多个前导字符以简化操作:

input.lstrip('x0')

format is handy for this: 格式对此很方便:

>>> print '{:x}'.format(0x000000007c90e8ab)
7c90e8ab

>>> print '{:x}'.format(0x000000007c9220fc)
7c9220fc

In this particular case you can just do 在这种情况下,您可以

your_input[10:]

You'll most likely want to properly parse this; 您很可能想正确地解析它; your idea of splitting on separation of non-zero does not seem safe at all. 您对非零分隔进行拆分的想法似乎一点都不安全。

Seems to be the XY problem . 似乎是XY问题

If the number of characters in a string is constant then you can use the following code. 如果字符串中的字符数是常量,则可以使用以下代码。

input = "0x000000007c9226fc"
output = input[10:]

Documentation 文献资料

Also, since you are using rpartition which is defined as 另外,由于您使用的rpartition 定义

str.rpartition(sep) str.rpartition(sep)
Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. 在最后一次出现sep时分割字符串,并返回一个三元组,其中包含分隔符之前的部分,分隔符本身以及分隔符之后的部分。 If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself. 如果找不到分隔符,则返回一个包含两个空字符串的三元组,然后是字符串本身。

Since your input can have multiple 0's, and rpartition only splits the last occurrence this a malfunction in your code. 由于您的输入可以有多个0,并且rpartition仅拆分最后一次出现的代码,这是代码中的故障。

Regular expression for 0x00000 or its type is (0x[0]+) and than replace it with space. 0x00000或其类型的正则表达式为(0x[0]+)然后将其替换为空格。

import re
st="0x000007c922433434000fc"
reg='(0x[0]+)'
rep=re.sub(reg, '',st)
print rep

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

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