Удалить нежелательную подстроку из строкового значения json в Python - PullRequest
0 голосов
/ 29 апреля 2019

У меня есть формат json data, как я могу удалить substring, например: {\\fSimSun|b0|i0|c134|p2; и \\pt8; ect. в 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

1 Ответ

1 голос
/ 29 апреля 2019

Ваш код содержит пару ошибок. Во-первых, вы должны экранировать обратную косую черту '\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;', '')
...