簡體   English   中英

如何使用 re.sub 刪除方括號內的重復文本塊?

[英]How do I use re.sub to remove a repeated block of text within square brackets?

我收到了來自 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 ,我認為刪除內容的唯一方法是將響應視為字符串並使用正則表達式刪除標簽。

無需使用正則表達式。 利用

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 證明

結果

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM