如何使用re.sub删除方括号内重复的文本块?

2024-05-19 00:06:51 发布

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

我从一个API得到一个响应,它是一个带有一些'key':'values'的伪字典,但大部分只是一个带有'key:values'的文本块。我用.json()将其转换为:

{'status': 'done', 'nextLogId': 'AQAAAXb', 'logs': [{'content': {'service': 't2pipeline', 'tags': ['tag1:value1', 'tag2:value2', 'tag3:value3'], 'timestamp': '2021-01-05T05:25:03.416Z', 'host': 'i-00e17', 'attributes': {'caller': 'psignal/state_machine.go:451', 'ts': 1609824303.416246, 'level': 'warn'}, 'message': 'psignal: Ignoring scte35 segmentation_descriptor (type:Program Start eventID:0 refUTC:Jan 5 05:25:02.387626333): there is an active segment with the same event_id'}, 'id': 'AQAAAXb'}, {'content': {'service': 't2pipeline', 'tags': ['tag1:value1', 'tag2:value2', 'tag3:value3'], 'timestamp': '2021-01-05T05:25:03.416Z', 'host': 'i-00e17', 'attributes': {'caller': 'psignal/state_machine.go:713', 't2': {'scte35': {'event_id': 0, 'event_ptr': '0xc009f32b40', 'seg_type_id': 16}}, 'ts': 1609824303.4161847, 'level': 'info'}, 'message': 'psignal: scte35 segdesc eventID:0 type:Program Start'}, 'id': 'AQAAAXb'}], 'requestId': 'OVZRd3hv'}

这里有两个条目,实际上会有更多条目

我将转换为带有json.dumps()的字符串

然后使用re.sub()从响应中删除'tags': [],并像这样返回字符串

res = re.sub(r'"tags": \[.*"\],\s', "", response_string)

问题是它只返回最后一个条目

print(res)

{"status": "done", "nextLogId": "AQAAAXb", "logs": [{"content": {"service": "t2pipeline", "timestamp": "2021-01-05T05:25:03.416Z", "host": "i-00e17b8e872ec7d05", "attributes": {"caller": "psignal/state_machine.go:713", "t2": {"scte35": {"event_id": 0, "event_ptr": "0xc009f32b40", "seg_type_id": 16}}, "ts": 1609824303.4161847, "level": "info"}, "message": "psignal: scte35 segdesc eventID:0 type:Program Start"}, "id": "AQAAAXb"}], "requestId": "OVZRd3hv"}

如何修改正则表达式,以便删除'tags': []的每个实例,并返回包含所有项的整个字符串

注意:由于我不能按键del,所以我认为删除内容的唯一方法是将响应视为字符串,并使用regex删除标记


Tags: 字符串eventidhosttypeservicetagscontent
1条回答
网友
1楼 · 发布于 2024-05-19 00:06:51

不需要使用正则表达式。使用

import json
res = {'status': 'done', 'nextLogId': 'AQAAAXb', 'logs': [{'content': {'service': 't2pipeline', 'tags': ['tag1:value1', 'tag2:value2', 'tag3:value3'], 'timestamp': '2021-01-05T05:25:03.416Z', 'host': 'i-00e17', 'attributes': {'caller': 'psignal/state_machine.go:451', 'ts': 1609824303.416246, 'level': 'warn'}, 'message': 'psignal: Ignoring scte35 segmentation_descriptor (type:Program Start eventID:0 refUTC:Jan  5 05:25:02.387626333): there is an active segment with the same event_id'}, 'id': 'AQAAAXb'}, {'content': {'service': 't2pipeline', 'tags': ['tag1:value1', 'tag2:value2', 'tag3:value3'], 'timestamp': '2021-01-05T05:25:03.416Z', 'host': 'i-00e17', 'attributes': {'caller': 'psignal/state_machine.go:713', 't2': {'scte35': {'event_id': 0, 'event_ptr': '0xc009f32b40', 'seg_type_id': 16}}, 'ts': 1609824303.4161847, 'level': 'info'}, 'message': 'psignal: scte35 segdesc eventID:0 type:Program Start'}, 'id': 'AQAAAXb'}], 'requestId': 'OVZRd3hv'}
for i in range(len(res['logs'])):
    del res['logs'][i]['content']['tags']
print(res)

Python proof

结果

{'status': 'done', 'nextLogId': 'AQAAAXb', 'logs': [{'content': {'service': 't2pipeline', 'timestamp': '2021-01-05T05:25:03.416Z', 'host': 'i-00e17', 'attributes': {'caller': 'psignal/state_machine.go:451', 'ts': 1609824303.416246, 'level': 'warn'}, 'message': 'psignal: Ignoring scte35 segmentation_descriptor (type:Program Start eventID:0 refUTC:Jan  5 05:25:02.387626333): there is an active segment with the same event_id'}, 'id': 'AQAAAXb'}, {'content': {'service': 't2pipeline', 'timestamp': '2021-01-05T05:25:03.416Z', 'host': 'i-00e17', 'attributes': {'caller': 'psignal/state_machine.go:713', 't2': {'scte35': {'event_id': 0, 'event_ptr': '0xc009f32b40', 'seg_type_id': 16}}, 'ts': 1609824303.4161847, 'level': 'info'}, 'message': 'psignal: scte35 segdesc eventID:0 type:Program Start'}, 'id': 'AQAAAXb'}], 'requestId': 'OVZRd3hv'}

相关问题 更多 >

    热门问题