Select to view content in your preferred language

Arcpy - Stripping out strings in a field seperated by comma (with nulls)

489
1
08-08-2018 12:30 AM
timdunlevie1
New Contributor II

Hi all,

I have a feature class which has 1 field which are hyperlinks to photos:

e.g. [PHOTO] : C:\temp\photo1.jpg, c:\temp\photo2.jpg, c:\temp\photo3,jpg

All one string in the PHOTO field, separated by commas.

I want to strip out each hyperlink and update 9 Hyperlink fields in the feature class: [HYPERLINK1], [HYPERLINK2], [HYPERLINK3]….[HYPERLINK9].

I thought it would be easy going something like this:

with arcpy.da.UpdateCursor(fc, ['PHOTOS', 'HYPERLINK1', 'HYPERLINK2', 'HYPERLINK3', 'HYPERLINK4', 'HYPERLINK5', 'HYPERLINK6', 'HYPERLINK7', 'HYPERLINK8', 'HYPERLINK9']) as cursor:
    for row in cursor:
        row[1] = row[0].split(",")[0]
        row[2] = row[0].split(",")[1]
        row[3] = row[0].split(",")[2]
        row[4] = row[0].split(",")[3]

         etc...
        cursor.updateRow(row)

But I'm getting an out of index range as the [PHOTOS] field has some nulls.

And also the [PHOTO] field can have between NULL and 9 Hyperlink strings separated by commas.

So the above code would work if there were 9 strings separated by commas.

Is there a way to get around this?

ArcGIS v10.6, windows 10

Thanks

Tags (1)
0 Kudos
1 Reply
DanPatterson_Retired
Deactivated User

As an example of one way to test.

rows = ["C:\temp\photo1.jpg, c:\temp\photo2.jpg, c:\temp\photo3,jpg",
        None,
        "C:\temp\photo4.jpg, c:\temp\photo5.jpg, c:\temp\photo6,jpg",
        "etc", 
        None]
        

rs = [[str(i).split(","), "no split"][i is None] for i in rows]

rs
Out[20]: 
[['C:\temp\\photo1.jpg', ' c:\temp\\photo2.jpg', ' c:\temp\\photo3', 'jpg'],
 'no split',
 ['C:\temp\\photo4.jpg', ' c:\temp\\photo5.jpg', ' c:\temp\\photo6', 'jpg'],
 ['etc'],
 'no split']

Now where it says "no split" you could put in a list of empty values equal to the length of your photo list or even just None.

You would have to show what you have so far in your code to make more suggestions

0 Kudos