0

I am importing a KMZ we receive as output from an iOS app into ArcMap using the KML to layer tool. The resulting table includes a "pop-up info" column that contians cells with marked up tags like:

<table><tr><td valign="top">Description</td><td>Stakes for monitoring 2 stakes</td></tr><tr bgcolor='3aafe8'><td valign="top">Time</td><td>N/A</td></tr><tr><td valign="top">Description</td><td>N/A</td></tr></table>

Is there a simple way to strip these html tags with the field calculator and VB Script / Python within ArcMap, or from the KMZ itself somehow prior to importing?

Thanks!

would a period at the end of a sentence interrupt the script? That's the only thing I noticed between the stopping point cells...

<table><tr><td valign="top">Description</td><td>View northwest toward point between alignment and high bank. Standing on alignment on left (left) bank of stream channel.</td></tr><tr bgcolor='3aafe8'><td valign="top">Time</td><td>N/A</td></tr><tr><td valign="top">Photos</td><td><img src="files/310.jpg"/><br/></td></tr></table>

  • I'm not sure why it's stopping midway. It's working on my end if you use the first code block with your error text. – Paul Aug 14 '13 at 23:09
1

I was able to extract Stakes for monitoring 2 stakes with the following code. I read over the official Python docs on the xml module:

from xml.etree.ElementTree import fromstring

kmz = '''<table><tr><td valign="top">Description</td><td>Stakes for monitoring 2 stakes
</td></tr><tr bgcolor='3aafe8'><td valign="top">Time</td><td>N/A</td></tr><tr>
<td valign="top">Description</td><td>N/A</td></tr></table>'''

extracted = fromstring(kmz)[0][1].text

You could use field calculator to perform this:

Parser:

Python

Code Block:

from xml.etree.ElementTree import fromstring
def parser(kmz):    
    try:
        return fromstring(kmz)[0][1].text
    except:
        return "Not Extracted!"

Expression:

parser(!field_name!)

I tested it and it works (for your example at least).

  • Hi Paul - thanks, that seems to be what I'm after. Could you point me in the right direction to be able to apply this to an entire column in a layer table? I have a whole column filled with lines like the example and would like to convert all of it to a new field in the same table, if possible. Thanks for any suggestions. – Michael Aug 13 '13 at 15:30
  • @Michael, assuming you can use calculate field on a table, my edited code should work. – Paul Aug 13 '13 at 15:40
  • Hi Paul - I get "a field name was not found or there were unbalanced quotation marks" – Michael Aug 13 '13 at 16:47
  • Actually i had that on VB, on Python I still get "Parsing error SyntaxError: invalid syntax (line 2)" – Michael Aug 13 '13 at 16:55
  • @Michael, you need to insert the field name that stores your data. – Paul Aug 13 '13 at 17:33
0

If there is a newline character in any of the rows being calculated, the Python parser on the Calculate Field tool will choke, producing the dreaded SyntaxError: EOL while scanning string literal error you came across, with no way of avoiding or even catching the error so you can at least skip that row and continue with the rest.

This is true all the way up to 10.1 SP1. I am not sure if they fixed it in 10.2.

See: Removing return/newline (\n) character from Field using Python and Field Calculator?

You'll have to either search for and fix these manually beforehand, or use an UpdateCursor within a Python script. You may also be able to use VBScript and MSXML/XPath but I have not tried this and it just sounds icky so I won't :)

  • 2
    Ew. That's not cool at all. – Paul Aug 15 '13 at 4:20
  • Thanks guys, I like hearing that it's sort of a bug vs. me just having a typo, etc. I'll look into the workaround you mentioned. Thanks for being patient Paul. – Michael Aug 15 '13 at 15:10

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.