从 Python 中 json 的字符串值中删除不需要的子字符串

Delete unwanted substring from string value of json in Python

我有一个 json 格式 data 如下,如何删除 substring 如:{\fSimSun|b0|i0|c134|p2;\pt8; 等。在 data['features']['properties']['Text'] 使用 Python?谢谢。

    data = {
    "type": "FeatureCollection",
    "name": "entities",
    "features": [
    { "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbBlockReference", "EntityHandle": "5F5" }, "geometry": { "type": "LineString", "coordinates": [ [ 2542.979171143861095, 1529.197520839406934 ], [ 2550.136112124296687, 1526.261271817294983 ], [ 2550.136112124296687, 1502.940228597864007 ], [ 2541.68221191783914, 1502.896517589801988 ], [ 2540.212335674471888, 1507.214248357231099 ], [ 2539.54868069528402, 1514.66504835861997 ] ] } },
{ "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbMText", "ExtendedEntity": "ACAD_MTEXT_COLUMN_INFO_BEGIN     75      2     79      0     76      1     78      0     48 15.87708675343719     49 12.5     50      1 0.0 ACAD_MTEXT_COLUMN_INFO_END", "EntityHandle": "628", "Text": "\pt8;name:2101;area:696.87;type:o" }, "geometry": { "type": "Point", "coordinates": [ 2546.429484481559939, 1502.996677372641898, 0.0 ] } },
    { "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbMText", "ExtendedEntity": "ACAD_MTEXT_COLUMN_INFO_BEGIN     75      2     79      0     76      1     78      0     48 7.751738177355946     49 12.5     50      1 0.0 ACAD_MTEXT_COLUMN_INFO_END", "EntityHandle": "5F5", "Text": "name:1802;area:214.94{\fSimSun|b0|i0|c134|p2;;type:o}" }, "geometry": { "type": "Point", "coordinates": [ 2540.415461913386935, 1515.353525792737173, 0.0 ] } },
    { "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbBlockReference", "EntityHandle": "5FB" }, "geometry": { "type": "LineString", "coordinates": [ [ 2541.68221191783914, 1502.896517589801988 ], [ 2550.136112124296687, 1502.940228597864007 ], [ 2550.142551061781887, 1501.694909916252072 ], [ 2551.273948108938839, 1501.694909916252072 ], [ 2551.273948108938839, 1491.808439367854817 ], [ 2546.558621786697131, 1495.0639439378931 ] ] } },
    { "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbMText", "ExtendedEntity": "ACAD_MTEXT_COLUMN_INFO_BEGIN     75      2     79      0     76      1     78      0     48 5.831716740670343     49 12.5     50      1 0.0 ACAD_MTEXT_COLUMN_INFO_END", "EntityHandle": "5FB", "Text": "name:1803;area:56.84;type:o" }, "geometry": { "type": "Point", "coordinates": [ 2543.846248643154013, 1501.07490155440496, 0.0 ] } },
    { "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbBlockReference", "EntityHandle": "601" }, "geometry": { "type": "LineString", "coordinates": [ [ 2551.273948108938839, 1502.21158770401712 ], [ 2551.273948108938839, 1491.808439367854817 ], [ 2552.261977759469119, 1491.126294629014865 ], [ 2552.261977759469119, 1493.240184109547044 ], [ 2559.346656675101713, 1493.240184109547044 ], [ 2559.346656675101713, 1491.228391537522839 ] ] } },
    { "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbMText", "ExtendedEntity": "ACAD_MTEXT_COLUMN_INFO_BEGIN     75      2     79      0     76      1     78      0     48 10.21067821324186     49 12.5     50      1 0.0 ACAD_MTEXT_COLUMN_INFO_END", "EntityHandle": "601", "Text": "name:1806;area:425.75;type:o" }, "geometry": { "type": "Point", "coordinates": [ 2553.99154355427163, 1498.554737846677881, 0.0 ] } }
    ]
    }

这是我目前尝试过的方法:

import json
features = data["features"]
for each in data['features']:
    try:
        text = each['properties']['Text']
        text1 = map(lambda x: x.replace('\pt8;','').replace('{\fSimSun|b0|i0|c134|p2;',''), text)
        print(text1)
    except:
        continue

您的代码有几个错误。首先,您必须转义反斜杠 '\f' 否则它会被解释为换页符。第二,你不保存替换结果。

for i in range(len(data['features'])):
    if 'Text' in data['features'][i]['properties']:
        data['features'][i]['properties']['Text'] \
            = data['features'][i]['properties']['Text']\
                  .replace('\pt8;', '')\
                  .replace('{\fSimSun|b0|i0|c134|p2;', '')