簡體   English   中英

如何刪除 Python 中兩個分隔符之間的文本

[英]How to remove text between two delimiters in Python

我正在嘗試刪除短語“segmentation”之后 [] 括號之間的所有文本:“請參閱文件中的以下片段以了解上下文。

 "annotations": [
        {
            "id": 1,
            "image_id": 1,
            "segmentation": [
                [
                    621.63,
                    1085.67,
                    621.63,
                    1344.71,
                    841.66,
                    1344.71,
                    841.66,
                    1085.67
                ]
            ],
            "iscrowd": 0,
            "bbox": [
                621.63,
                1085.67,
                220.02999999999997,
                259.03999999999996
            ],
            "area": 56996,
            "category_id": 1124044
        },
        {
            "id": 2,
            "image_id": 1,
            "segmentation": [
                [
                    887.62,
                    1355.7,
                    887.62,
                    1615.54,
                    1114.64,
                    1615.54,
                    1114.64,
                    1355.7
                ]
            ],
            "iscrowd": 0,
            "bbox": [
                887.62,
                1355.7,
                227.0200000000001,
                259.8399999999999
            ],
            "area": 58988,
            "category_id": 1124044
        },
        {
            "id": 3,
            "image_id": 1,
            "segmentation": [
                [
                    1157.61,
                    1411.84,
                    1157.61,
                    1661.63,
                    1404.89,
                    1661.63,
                    1404.89,
                    1411.84
                ]
            ],
            "iscrowd": 0,
            "bbox": [
                1157.61,
                1411.84,
                247.2800000000002,
                249.7900000000002
            ],
            "area": 61768,
            "category_id": 1124044
        },
        ........... and so on.....

我最終只想在出現分詞后刪除方括號之間的所有文本。 換句話說,output 看起來像(對於第一個實例):

"annotations": [
            {
                "id": 1,
                "image_id": 1,
                "segmentation": [],
                "iscrowd": 0,
                "bbox": [
                    621.63,
                    1085.67,
                    220.02999999999997,
                    259.03999999999996
                ],
                "area": 56996,
                "category_id": 1124044
            },

我試過使用下面的代碼,但目前運氣不太好。 由於新線路,我有什么問題嗎?

import re
f = open('samplfile.json')
text = f.read()
f.close()

clean = re.sub('"segmentation":(.*)\]', '', text)

print(clean)

f = open('cleanedfile.json', 'w')
f.write(clean)
f.close()

我明白我對干凈行中 [s 的確切定位可能不太正確,但這段代碼目前沒有刪除任何內容。

Python 有一個內置的json模塊,用於解析和修改 JSON。正則表達式可能很脆弱,而且比它的價值更讓人頭疼。

您可以執行以下操作:

import json

with open('samplfile.json') as input_file, open('output.json', 'w') as output_file:
    data = json.load(input_file)
    for i in range(len(data['annotations'])):
        data['annotations'][i]['segmentation'] = []

    json.dump(data, output_file, indent=4)

然后, output.json包含:

{
    "annotations": [
        {
            "id": 1,
            "image_id": 1,
            "segmentation": [],
            "iscrowd": 0,
            "bbox": [
                621.63,
                1085.67,
                220.02999999999997,
                259.03999999999996
            ],
            "area": 56996,
            "category_id": 1124044
        },
        {
            "id": 2,
            "image_id": 1,
            "segmentation": [],
            "iscrowd": 0,
            "bbox": [
                887.62,
                1355.7,
                227.0200000000001,
                259.8399999999999
            ],
            "area": 58988,
            "category_id": 1124044
        },
        {
            "id": 3,
            "image_id": 1,
            "segmentation": [],
            "iscrowd": 0,
            "bbox": [
                1157.61,
                1411.84,
                247.2800000000002,
                249.7900000000002
            ],
            "area": 61768,
            "category_id": 1124044
        }
    ]
}

您的方法大部分是正確的,但 Python 正則表達式不接受\n作為. , 要修復它,請在 re.sub() 中添加flags=re.DOTALL作為參數。

順便說一下,您可能需要在正則表達式中使用\"而不是"

暫無
暫無

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

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