Select to view content in your preferred language

Using Calculate Field in python?

1691
20
01-06-2011 08:18 AM
BartłomiejStaroń
Occasional Contributor
The tool works well in the MODEL bilder, but when it wants to convert it to the python script error occurs. All data from the column disappear.



import arcgisscripting
gp = arcgisscripting.create(9.3)
gp.workspace = "D:\\DATA.gdb\\"
TABLE = "D:\\DATA.gdb\\TABLE"
FIELD = "texx"

gp.CalculateField_management("TABLE", "FIELD", "StrConv([texx],vbUpperCase)", "VB")



What am I doing wrong??
0 Kudos
20 Replies
JasonScheirer
Esri Alum
Don't quote the first two params:


gp.CalculateField_management(TABLE, FIELD, "StrConv([texx],vbUpperCase)", "VB")
0 Kudos
BartłomiejStaroń
Occasional Contributor
Still not working:(


ExecuteError: ERROR 999999: Error executing function.
Type mismatch: 'StrConv'
Failed to execute (CalculateField)
0 Kudos
BartłomiejStaroń
Occasional Contributor
Maybe I should use the cursor objects?
0 Kudos
JoeVondracek
Regular Contributor
How about trying it with Python, since it is a Python script?
gp.CalculateField_management(TABLE, "texx", "str(!texx!).upper()", "PYTHON", "")
0 Kudos
BartłomiejStaroń
Occasional Contributor
I came up with something like this:

cur = gp.UpdateCursor("sfora_2")
row = cur.Next()

while row:
    row.texx = str.capitalize(row.texx)
   
print "end"

But it only replaces the first letter capitalized.
I want to have every word that begins with a capital letter.
I have some ideas but I do not know how to implement it into the script.


import re
w= "it should work"
bbb = re.split('([ ] *)', w)
''.join([each.capitalize() for each in bbb])
print ''.join([each.capitalize() for each in bbb])
0 Kudos
ChrisSnyder
Honored Contributor
It would look something like this:

updateRows = gp.UpdateCursor("sfora_2")
updateRow = updateRows.Next()
while updateRow:
   newString = ""
   for word  in updateRow.MYFIELD.split(" "):
      newString = item.capitalize() + " " + newString
   updateRow.MYFIELD = newString[:-1]
updateRow = updateRows.Next()
del updateRow
del updateRows


This code would take a field value of "hello world", and trun it into "Hello World".

I think I am misunderstanding what you want to do though....

If you have a string "hello world", what do you want it to look like?

If you want everything to be upper case (e.g. "HELLO WORLD"), do this:

updateRows = gp.UpdateCursor("sfora_2")
updateRow = updateRows.Next()
while updateRow:
   updateRow.MYFIELD = updateRow.MYFIELD.upper() #.upper() capitalizes every character
   updateRow = updateRows.Next()
del updateRow
del updateRows
0 Kudos
BartłomiejStaroń
Occasional Contributor
The first script is what I was looking for.
However, I will get an error:
NameError: name 'item' is not defined

Recently I started learning Python, so sorry to ask some simple questions.
0 Kudos
ChrisSnyder
Honored Contributor
Woops....

updateRows = gp.UpdateCursor("sfora_2")
updateRow = updateRows.Next()
while updateRow:
   newString = ""
   for word  in updateRow.MYFIELD.split(" "):
      newString = word.capitalize() + " " + newString
   updateRow.MYFIELD = newString[:-1]
updateRow = updateRows.Next()
del updateRow
del updateRows
0 Kudos
BartłomiejStaroń
Occasional Contributor
I could have guessed.
Thanks a lot.
I hope that the next script will not need much help.
0 Kudos