Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Any way to simplify this code?

$
0
0
My program runs fine, but I keep thinking there must be a way to simplify this code. I have a intersections.shp with all the street names that intersect in one field 'NAMES'. This separates them into individual fields, i.e. STREET1, STREET2, etc...

Is there a better way to write this or should I just rejoice in that it runs?
Code:

AddField_management(spatial_join, 'STREET1', 'TEXT')
    AddField_management(spatial_join, 'STREET2', 'TEXT')
    AddField_management(spatial_join, 'STREET3', 'TEXT')
    AddField_management(spatial_join, 'STREET4', 'TEXT')
    AddField_management(spatial_join, 'STREET5', 'TEXT')#if more than 5 streets intersect, need to add more fields
    urows = UpdateCursor(spatial_join)
    for urow in urows:
        array = urow.NAMES.split('&')
        urow.STREET1 = array[0]
        urow.STREET2 = array[1]
        urow.STREET3 = ''
        urow.STREET4 = ''
        urow.STREET5 = ''
        if len(array) == 5:
            urow.STREET3 = array[2]
            urow.STREET4 = array[3]
            urow.STREET5 = array[4]
        elif len(array) == 4:
            urow.STREET3 = array[2]
            urow.STREET4 = array[3]
        elif len(array) == 3:
            urow.STREET3 == array[2]
        urows.updateRow(urow)
    del urows, urow

Thanks,

Bryce

Viewing all articles
Browse latest Browse all 2485

Trending Articles