Using the .split() to grab attribute from KMZ

605
5
09-24-2020 02:28 PM
Tim-Woodfield
New Contributor III

Hi everyone,

I am trying to use the .split() function on a string and keep gettings errors. The field is the popup information from a converted KMZ and is called PopupInfo. The code I am trying to use is:

!PopupInfo!.split("<br>")[14]

but am getting Error 000539 and is saying "IndexError: list index out of range". The calculation will run fine if 14 is swapped out for 0 or -1 to get the first or last section. I am counting 27 sections if it is being broken at each "<br>". I've tried finding and replacing all of the <br>'s with a dash or underscore but the calculation still fails with anything other than 0 or -1. The text string that I am trying to pull from looks something like this: 

US20 EB    <br>       <br>   FID   57    <br>   RampDate   9/30/2013    <br>   RouteId   161    <br>   RouteName   US20 EB    <br>   TownId   314    <br>   TownName   Watertown    <br>   IsMissingR   False    <br>   IsMissingS   False    <br>   IsMissingC   True    <br>   InspNotes       <br>   DistrictId   6    <br>   LegacyID   335    <br>   status   6    <br>   StatusText   Deficient    <br>   DstrctRank   669    <br>   deficient   -1    <br>   GlobalID   {C4BBAA58-9DFE-4953-AC74-FF3448002388}    <br>   created_us       <br>   created_da       <br>   last_edite   DAVID.DINOCCO    <br>   last_edi_1   10/17/2017    <br>   ReInspectn   0    <br>   AssetID       <br>   ProgNotes       <br>   Rank   669

I would be trying to field calc a different field with the LegacyID from this PopupInfo. Is there a problem with this string that is causing the index errors? The lengths at both ends are never the same so I can't trim off the ends.

Thanks for the help!

Tim

0 Kudos
5 Replies
Arne_Gelfert
Regular Contributor

Works for me when I try this:

aString = "US20 EB    <br>       <br>   FID   57    <br>   RampDate   9/30/2013    \
<br>   RouteId   161    <br>   RouteName   US20 EB    <br>   TownId   314    <br>   \
TownName   Watertown    <br>   IsMissingR   False    <br>   IsMissingS   False    <br>  \
IsMissingC   True    <br>   InspNotes       <br>   DistrictId   6    <br>   LegacyID  \
335    <br>   status   6    <br>   StatusText   Deficient    <br>   DstrctRank   669  \
<br>   deficient   -1    <br>   GlobalID   {C4BBAA58-9DFE-4953-AC74-FF3448002388}   \
<br>   created_us       <br>   created_da       <br>   last_edite   DAVID.DINOCCO   \
<br>   last_edi_1   10/17/2017    <br>   ReInspectn   0    <br>   AssetID       <br> \
ProgNotes       <br>   Rank   669"

aString.split("<br>")[14]
>>> '   status   6    '‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

len(aString.split("<br>"))
>>>27

Something wrong with the field name? Is it case sensitive? If [0] and [-1] work maybe you're indexing a list with length 1?

Tim-Woodfield
New Contributor III

Figured it out! One of my rows had an empty PopupInfo field so it was failing because there weren't enough indexes. So yes you were right that the index was only a length of 1. I only selected the features with the PopupInfo filled out and it worked.

Thanks!

0 Kudos
DanPatterson
MVP Esteemed Contributor
aString.split("<br>")[14] if aString.count("<br>") >= 50 else "nope"
'nope'

aString.split("<br>")[14] if aString.count("<br>") >= 14 else "nope"
'   status   6    '

maybe you have a shorter string than 14 <br> values


... sort of retired...
Tim-Woodfield
New Contributor III

Figured it out! One of my rows had an empty PopupInfo field so it was failing because there weren't enough indexes. So yes you were right that the index was only a length of 1. I only selected the features with the PopupInfo filled out and it worked.

Thanks!

0 Kudos
Arne_Gelfert
Regular Contributor

Glad it worked. Mark is as Correct.

0 Kudos