I have an attribute field formatted using HTML all ready to go for Google Earth markers.

Here is the value:

    Facility type: 
    Geoid: <a href="http://www.prisonersofthecensus.org/data/2010blocks/261450002001020/">261450002001020</a>
    County: Saginaw County
    2010 Correctional Population: 18

enter image description here

I tried to paste the actual HTML but couldn't figure out how to display it as text rather than formatted html!

I've love to have separate attribute fields for the columns within that string:

Facility Type
2010 Correctional Population

Can this be done using Python?


1 Answer 1


Assuming python and you want as a result a dict

{ "Facility Type" : None, 
  "Geoid": "<a ...",
  "County" : "Saginaw County",
  "2010 Correctional Population": 18

you could use

mystr = "<div>Facility type: </div><div>Geoid: <blah></div><div>County: Saginaw County</div><div>Pop: 18</div>"
parts = mystr.replace("<div>","").split("</div>")
r = {}
for part in parts:
    kv = part.split(":")

    if kv[0].strip() != "":
        r[kv[0]] = kv[1].strip()

Result is {'Facility type': '', 'County': 'Saginaw County', 'Geoid': '<blah>', 'Pop': '18'}

This does not handle the type conversion of the appropriate attributes, but that can be done.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.