mercredi 24 juin 2020

Python 2.7 Append results from multiple 'if' statements to a single list. Works correctly only on first if statement

I'm trying to populate an empty list with new formatted labels for map use and exporting to a table. The format needs to be different for different regions (defined by the first two digits in ADMIN_ORG). The labels I need to format are in the ID field.

The first 'if' statement (startswith '04') seems to print correct results. My next 'elif' statement (startswith '10') does not work right. The code using labels.extend is adding in data but only one character at a time to the list.

Example: 'D', 'O', 'U', 'G', 'L', 'A', 'S', '_', 'H', 'I', 'G', 'H', 'W', 'A', 'Y', '6', '5', '4' It should be 'DOUGLAS_HIGHWAY' OR '654'

I have also tried using labels.append for the next 'elif' but it doesn't append anything from that 'elif'.

I need to have at least 12 or so different 'if' iterations for my labels list.

What am I missing?

import arcpy
from arcpy import env
import re, string, time
from string import ascii_letters

arcpy.env.overwriteOutput = 1
start = time.time()

#********DATA AND PATHS********

mxd = arcpy.mapping.MapDocument(r'D:\JJ_Development\Projects\Basemap\Transportation\Labels.mxd')
fc = arcpy.mapping.ListLayers(mxd, 'RoadCore_Existing')[0]

fields = ['ID', 'ADMIN_ORG']
labels = []

def newlabel():
    with arcpy.da.SearchCursor(fc, fields) as sc:
        for row in sc:
            if row[1].startswith('04'):
                labels.append('{}'.format(row[0].replace("-ADMIN","").replace('-A-ADMIN','').replace('-PARKING','')[2:].lstrip('0').replace('.', '-').lstrip(ascii_letters)))
            elif row[1].startswith('10'):
                labels.extend('{}'.format(row[0].lstrip('0').replace('.', '-')))#.lstrip(ascii_letters)))
            #else:
                #labels.extend(row[0])
    print(labels)

Aucun commentaire:

Enregistrer un commentaire