How to split a field into multiple rows with python based on first upper case letter

472
4
02-08-2023 12:15 PM
KathleenHoenke
Occasional Contributor

I have a table with multiple rows, row a and row b. Row a has several species names separated by an enter key and a capital letter. I want to separate each species into its own row, and replicate row b. Here is the code I'm using, but I'm not sure how to make it upper case. 

import arcpy
import os
import sys
from arcpy import env
from arcpy.sa import Con
from arcpy.sa import *

SC_table = r'D:\Data\Biological\SC\SC 2022\SC_2022.gdb\SC_table_2023'
new_sc_table = r'D:\Analysis\Biological_Update\Occurrences_Updates\December_2022_Updates\OUTPUT\Species_TablesbyState_2022.gdb\SC_table_Schema_2023'

searchC = arcpy.da.SearchCursor(SC_table, ("spp_list", "HUC12"))  # plus other attribute if you need

insertC = arcpy.da.InsertCursor(new_sc_table, ("spp_list", "HUC12"))

for row in searchC:
    print(row[0])
    A = [x.split(" ") for x in row[0]

    for i in range(min(len(A[0]), len(A[1]))):
        newRow = [A[0][i], A[1][i]]
        insertC.insertRow(newRow)
del insertC

 

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

in python ... yourstring.upper()

ps you are missing a  closing ] on line 17

[x.split(" ") for x in row[0]]  # -- added ]

... sort of retired...
0 Kudos
KathleenHoenke
Occasional Contributor

Thank you! Sorry, how can that be integrated into the script above? 

0 Kudos
DanPatterson
MVP Esteemed Contributor

perhaps showing a typical input and desired output would help, but as a guess for now

r = ["row0 value here"]

[(x.upper()).split(" ") for x in r]
# -- yields
[['ROW0', 'VALUE', 'HERE']]

... sort of retired...
0 Kudos
JonathanNeal
Esri Contributor

@KathleenHoenke Would this code snip help you?  It finds words in a string that start with capital leters.

import re
text = "HelloWorldHowAreYou"
# Regular expression pattern to split the string using capital letters
pattern = r"[A-Z][a-z]*"
result = re.findall(pattern, text)
print(result) #['Hello', 'World', 'How', 'Are', 'You']
0 Kudos