Here is a python crash course for the field calculator (assuming ArcGIS 10):First of all python is just as easy as VB (if not easier). It's just a different language, so the syntax is not identical to VB. That means that you can't write VB in python, just as you can't write C++, Perl or C# in python, so yes you need to learn a little python to write python.Second: The field calculator is actually extending the python language with the notation of "fields names" just as it did with VB. In VB field calculator you quote field names with sqaure brackets: [field_name]. In python you do the same, but with exclamation marks instead: !field_name!. That is because square brackets already have a different meaning in python.Third: String formatting in python is much more powerful than i VB, and also very different from VB. To format a string in python one creates a "format string" and then use the format operator:
"This is a %s with the integer %d and the float %.4f" % ('format string', 42, 3.14159265)
Fourth: To pick substring from a string, one use index and slicing:
"mississippi"[0] # -> "m"
"mississippi"[:4] # -> "miss" # End at the 4th character
"mississippi"[-4:] # ->"ippi" # Start is 4th from the right
"mississippi"[::-1] # -> "ippississim" # Stepping backwards
"mississippi"[1::3] # -> "iiii" # Start at 1, and step 3
So for some field calculator examples:To copy the contents from one field to another, just quote the source field name in exclamation marks:
!source_field!
To copy and format floating point numbers to a text field, use a format string:
"%.3f" % !source_float_field!
To fill a column with the time of day, formatted in 12 hour format:First check the "Show codeblock" checkbox, then in the Pre-Logic Script Code box enter the following:
import datetime
t = datetime.datetime.now()
Then in the calc field code box below enter the following:
t.strftime('%I:%M:%S %p) # See python help for strftime for details
Given a float field with POSIX timestamps (seconds since epoch). To convert that to a date and time string in ISO format (2010-01-15 06:43:12):in the pre-logic box enter:
import datetime
and in the calc window enter:
datetime.datetime.fromtimestamp(!time_stamp_field!).strftime('%Y-%m-%d %H:%M:%S')
Try these examples out and then come back with more specific questions about the Calculate Field functionallity. Also keep in mind that some people here (me included), might not be fluent in VB, so examples of what you expect specific VB code snippets to do is helpful.