Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Does anyone know how to use the .upper() in a stand alone python script so it can be used to change all characters in a table into UPPER case? I don't want to use the field calculator since I have multiple tables, etc...

I currently am using a cursor to correct mistakes in my tables, but can't figure out the correct syntax to use it to convert all the strings to UPPER case!

share|improve this question
1  
2  
I think the user is asking how to convert every field in a table to upper case in one move. No? –  the_skua Apr 10 at 17:40
    
Correct. So, something similar to the VB code in the field calculator - UCase(fieldname)...but I want to do it to the whole table using an update cursor...I can't figure out what to put before the .upper()...I've listed my fields, and now want to write something like - if row[0] is (something), then upper case. –  Dane Apr 10 at 17:42

4 Answers 4

up vote 8 down vote accepted

The following example shows how to integrate the built-in python method .upper() with the arcpy update cursor. This example first tests if a field is of type String then checks each row within that string for lowercase values. If there are lower case values, the row is updated with all upper case.

import arcpy

fc = r'C:\temp\test.gdb\yourFC'

desc = arcpy.Describe(fc)
fields = desc.fields

for field in fields:
    if field.Type == "String":
        with arcpy.da.UpdateCursor(fc, str(field.name)) as cursor:
            for row in cursor:
                if row[0] == None:  # Check for "<Null>"
                    continue
                else:
                    any(x.islower() for x in row[0]) == True
                    row[0] = row[0].upper()
                    cursor.updateRow(row)
share|improve this answer
    
Aaron,thank you! This is exactly what I was looking for! The only thing I noticed is that NULL values are not iterable, so I ran a quick script to get these blank. Out of curiosity, do you know how to jump over those NULL values in this script? –  Dane Apr 10 at 19:58
    
No problem, I have updated the script with logic to check for <Null>. –  Aaron Apr 10 at 20:17
2  
Really impressive. I've learned a lot in this little script. Thank you very much! –  Dane Apr 10 at 20:21

Here is how you can use .upper() in python:

data = "lower case data"
upper_case_data = data.upper()
print upper_case_data

it can also be used directly with a string

print "lower case data".upper()
share|improve this answer
    
Thanks, Kyle. Do you know how I would use this with a search cursor? I'm going through each field in my table and if there are any lower case characters, I want them to change to upper case. –  Dane Apr 10 at 17:51
    
@Dane What database system are you using? –  kyle k Apr 10 at 18:15
    
I'm using Arcmap 10.1 and writing my code in PythonWin. –  Dane Apr 10 at 18:21

Here's a quick example:

fc = "whatever"
fields = ("Name", "Addy", "Stuff")
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        row[0] = row[0].upper()
        row[1] = row[1].upper()
        row[2] = row[2].upper()
        cursor.updateRow(row)

And here's the ArcGIS manual for the da.UpdateCursor

share|improve this answer
    
Thanks for the help! I've realized that the issue I'm having is having the cursor hit each field. I don't want to have to list all the field names...The manual for the da.UpdateCursor says to use (*) if you want all field names to be used, but I can never get that to work...seeing you reinforce the row[0] = row[0].upper() has definately helped...I'd vote it up, but I don't have enough street credit! –  Dane Apr 10 at 19:02

Similar, but different to above:

import arcpy
fc = "YOUR FC"
fields = [f.name for f in arcpy.ListFields(fc) if f.Type == "String"]

cur = arcpy.UpdateCursor(fc)
for row in cur:
    for field in fields:
        try:
            row.setValue(field) = row.getValue(field).upper()
        except:
            pass
    row.updateCursor(row)
share|improve this answer
    
Thanks, Darryl! Any particular reason you didn't use arcpy.da.UpdateCursor? I'm at home and don't have my data, but I'm assuming that the "pass" would prevent the NULL iterable error...nice. –  Dane Apr 11 at 0:25
1  
Oh, yeah, I am still running 10.0 - so don't have the arcpy.da.UpdateCursor available yet. I know that this is faster - so that change will improve you efficiency. Also, I would imagine that running through the cursor 1 time will be faster than running through it 1 time for each field as the accepted answer shows. Hope it works for you! –  dklassen Apr 11 at 16:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.