从更大的字符串中提取所需子字符串的pythonic方法

2024-06-28 20:06:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一根这样的绳子

msg = b'@\x06string\x083http://schemas.microsoft.com/2003/10/Serialization/\x9a\x05\x18{"PUID":"9279565","Title":"Risk Manager","Description":"<strong>Risk Manager </strong><br />\\n<br />\\nLentech, Inc. is currently seekinga Risk Manager inGreenbelt,"}\x01'

字符串{"PUID":"9279565","Title":"Risk Manager","Description":"<strong>Risk Manager </strong><br />\\n<br />\\nLentech, Inc. is currently seekinga Risk Manager inGreenbelt,"}json parsable。因此,我编写了以下代码来从上面的msg中删除垃圾字符串

x1 =  msg.split(b'{"',1)[1]
>>> 
>>> x1
b'PUID":"9279565","Title":"Risk Manager","Description":"<strong>Risk Manager </strong><br />\\n<br />\\nLentech, Inc. is currently seekinga Risk Manager inGreenbelt,"}\x01'
x2 = x1[::-1].split(b'}"', 1)[1][::-1]
>>> x2
b'PUID":"9279565","Title":"Risk Manager","Description":"<strong>Risk Manager </strong><br />\\n<br />\\nLentech, Inc. is currently seekinga Risk Manager inGreenbelt,'
>>> final_msg = b'{"%s"}'%x2
>>> final_msg
b'{"PUID":"9279565","Title":"Risk Manager","Description":"<strong>Risk Manager </strong><br />\\n<br />\\nLentech, Inc. is currently seekinga Risk Manager inGreenbelt,"}'
>>> import json
>>> json.loads(final_msg)
{'Description': "<strong>Risk Manager </strong><br />\\n<br />\\nLentech, Inc. is currently seekinga Risk Manager inGreenbelt,'", 'Title': 'Risk Manager', "b'PUID": '9279565'}

这是一个不好的方式做什么是需要的,我想知道一个更优化的方式来实现这个结果。我认为正则表达式在这里很有用,但我对正则表达式的知识非常有限。你知道吗

提前谢谢


Tags: brjsontitleismanagermsgdescriptioninc
2条回答

可以先将字节类型转换为字符串类型

msg = str(msg)

之后,您可以编写一个生成器函数和枚举,以拉出正在搜索的符号的索引

def gen_index(a_string):
    for i,symbol in enumerate(a_string):
        if symbol == '{':
            yield i
    for j , symbol in enumerate(a_string):
       if symbol == '}':
           yield j

 >>>a = list(gen_index(msg))  # returns the array
 >>># use array slicing to output to json. We need the first occurance of '{' and the last occurance of '}'
 import json
 json_output = json.loads(msg[a[0]:a[-1]+1])

给你:

import re
final_msg = re.search("{.*}", msg).group(0)

相关问题 更多 >