I have an attribute field formatted using HTML all ready to go for google earth markers.

The KML in question is here:


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 seperate attribute fields for the columns within that string:

Facility Type
2010 Correctional Population

How can this be done?

QGIS? LibreOffice? Python?



  • If I read this correctly you want something like a table with one column the field names, the other the attribute values? – om_henners Mar 28 '13 at 1:22
  • That's right, Om... – DPSSpatial Apr 2 '13 at 16:03
  • 2
    If you want your code to show up as a codeblock, you have to indent it 4 spaces. The easiest way is to paste your code in, then highlight it, then press Ctrl + k. This will automatically indent the whole block. For more info: gis.stackexchange.com/editing-help – Jay Guarneri Apr 2 '13 at 16:29
  • What do you mean by "seperate attribute fields"? How should they show up? Where? – til_b Apr 3 '13 at 6:59
  • @til_b see below for CSV creation... – DPSSpatial Apr 3 '13 at 15:29

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.

| improve this answer | |
  • Til_b - that is really confusing so I'll just post the KML ... – DPSSpatial Apr 2 '13 at 16:04
  • 1
    At pastebin.com/1YsqZqbp I wrote a version that grabs the values directly from your KML. I am still not sure what you want to do, but you can do it now yourself. See for example the stuff I do with the results at the end of the paste. – til_b Apr 3 '13 at 9:35
  • @til_b I greatly appreciate this! First off, what I'd ultimately be after is a CSV of the name, prisoner count, and lat, lon so I can replot / convert to SHP...this is a good learning tool for me, though, as I finally got aptana running as a python IDE to make your script work (thank you again!). If you have any thoughts about CSV creation, let me know. Anything helps, and as usual, teaches me a lot!!! Thank you again!!! – DPSSpatial Apr 3 '13 at 15:29
  • 1
    @mapBaker at pastebin.com/KKJj2Z2M there is a new version. This version also grabs the lat and lon values from the KML location element and stores them in the jail dict. For CSV Export jou just have to adapt line 59 (which prints the results in a specified format). – til_b Apr 4 '13 at 9:07
  • @til_b you're a straight up python ninja. THANK YOU!!! I was able to create a CSV from this script...When I get this published, I want to give you some credit if you'd like...basically the data can't be formatted from the source database kept by prisonersofthecensus.org...so this was the only way to get it into 'data'...if you could send me an email: matt baker all one word at gmail dot com – DPSSpatial Apr 8 '13 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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