我的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
答案 0 :(得分:1)
您的代码有几个错误。首先,您必须转义反斜杠dn: dc=R2D2
dc: R2D2
objectClass: dcObject
objectClass: country
objectClass: top
c: DE
dn: o=TD,dc=R2D2
o: TD
objectClass: organization
objectClass: top
,否则它将被解释为换页符。其次,您不保存替换结果。
'\f'