【问题标题】:String missed while splitting a string in python在python中拆分字符串时丢失了字符串
【发布时间】:2013-09-11 20:54:51
【问题描述】:

我正在尝试拆分这个 udp 数据包,我想要特定的数据包包含“xx ff”

0000  xx ff 3a 31 89 c3 ff 00 58 00 20 41 aa aa 03 00   >.:1....X. A....
0010  02 de 01 11 05 00 02 00 00 ff 3c ff 00 00 34 2d   .........K<....-
0020  00 44 00 00 00 00 00 00 0a x3 00 01 00 60 00 00   ................
0030  00 00 89 70 62 00 02 00 00 76 98 05 8b            ..i.b....v...

所以:

if '0  ' in line:print line;
    line = line.split('0  ')[1]

产量:

0000  xx ff 3a 31 89 c3 ff 00 58 00 20 41 aa aa 03 0   >.:1....X. A....
0010  02 de 01 11 05 00 02 00 00 ff 3c ff 00 00 34 2d   .........K<....-
0020  00 44 00 00 00 00 00 00 0a x3 00 01 00 60 00 0   ................
0030  00 00 89 70 62 00 02 00 00 76 98 05 8b            ..i.b....v...\

每行缺少一个尾随零

【问题讨论】:

标签: python


【解决方案1】:

其中一个可能会有所帮助:

packet='''0000  xx ff 3a 31 89 c3 ff 00 58 00 20 41 aa aa 03 00   >.:1....X. A....
0010  02 de 01 11 05 00 02 00 00 ff 3c ff 00 00 34 2d   .........K<....-
0020  00 44 00 00 00 00 00 00 0a x3 00 01 00 60 00 00   ................
0030  00 00 89 70 62 00 02 00 00 76 98 05 8b            ..i.b....v...'''


# Using a generator expression
result =  ' '.join(' '.join(line[5:55].split()) for line in packet.splitlines())
print 'xx ff' in result  # true
print 'x3' in result     # true
print '72' in result     # false

# Using a loop
result = []
for line in packet.splitlines():
    result.extend(line[5:55].split())
result = ' '.join(result)
print 'xx ff' in result  # true
print 'x3' in result     # true
print '72' in result     # false

【讨论】:

    【解决方案2】:

    您将在多次出现的字符串上分割线。注意:

    >>> s = "0000  xx ff 3a 31 89 c3 ff 00 58 00 20 41 aa aa 03 0   >.:1....X. A...."
    >>> s.split("0  ")
    ['000', 'xx ff 3a 31 89 c3 ff 00 58 00 20 41 aa aa 03 ', ' >.:1....X. A....']
    

    当您拆分字符串时,您拆分的字符串不包含在结果列表元素中。在这种情况下,您可以将其限制为仅在第一次出现时拆分:

    >>> s.split("0  ", maxsplit=1)
    ['000', 'xx ff 3a 31 89 c3 ff 00 58 00 20 41 aa aa 03 0   >.:1....X. A....']
    

    这可能仍然不是您想要的,因为现在列表的第二个元素仍然包含 ASCII 转储部分。在这种情况下,您可能需要再次拆分 hex 转储和 ASCII 转储之间出现的多个空格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 2010-09-19
      • 2010-09-30
      • 1970-01-01
      相关资源
      最近更新 更多