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.

Below is how I used the Update Cursor in my final script. As you can see getValue was used a few times, perhaps there is a cleaner way to do this? But my concatenated field wouldn't populate properly otherwise.

    sourceRows = arcpy.UpdateCursor(inFeatures)
    source = sourceRows.next() 

    while source:
        fieldString1 = source.getValue(fieldName1)
        fieldString2 = source.getValue(fieldName2)
        concatString= (str(source.getValue(fieldName2))[1:7] + str(source.getValue(fieldName1))[1:7])
        source.setValue(fieldName3, concatString)
        source = sourceRows.next()
    del source, sourceRows

I have a script that creates three fields in a feature class. The script adds the X and Y coordinates to the first two fields. I would like data found in the middle parts of the first two fields to populate the third field. Does anyone know how to find the middle part of a field using Python? I see where VBScript seems to have a MID function but I haven't found anything for Python yet. Below is what the third field (fieldName3) should look like.

fieldName1     fieldName2   fieldName3
12345.321       36789.321   67892345
24680.5425      98765.432   87654680

Do you see how I want data to be combined in fieldName3? Below is the script as I have it now.

import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True

env.workspace = "D:/gisfiles/test.gdb/"

inFeatures = "fc"
fieldName1 = "xCoord"
fieldName2 = "yCoord"
fieldName3 = "Combined"
fieldPrecision = 18
fieldScale = 11

expression1 = "float(!SHAPE.CENTROID!.split()[0])"
expression2 = "float(!SHAPE.CENTROID!.split()[1])"

arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE", fieldPrecision, fieldScale)
arcpy.AddField_management(inFeatures, fieldName2, "DOUBLE", fieldPrecision, fieldScale)
arcpy.AddField_management(inFeatures, fieldName3, "DOUBLE", fieldPrecision, fieldScale)

arcpy.CalculateField_management(inFeatures, fieldName1, expression1,
arcpy.CalculateField_management(inFeatures, fieldName2, expression2,

sourceRows = arcpy.UpdateCursor(inFeatures)
source = sourceRows.next()

while source:
        **#Instead of 12345 I need an expression to combine fieldName1 and fieldName2**
        source.setValue(fieldName3, "12345")
        source = sourceRows.next()
del source, sourceRows
share|improve this question
You could slice out the parts you want, then concatentate them. –  Chad Cooper Oct 6 '11 at 23:44
add comment

3 Answers

up vote 3 down vote accepted

You can populate fieldName3 by using string slicing and concatenation, as Chap Cooper recommended.

Here is essentially what you want to do:

while source:
    source.setValue(fieldName3, str(fieldname2)[1:5] + str(fieldname1)[1:5])
    source = sourceRows.next()
del source, sourceRows
share|improve this answer
If fieldname1 and fieldname2 are floating point numbers, then expect "TypeError: 'float' object is not subscriptable". To avoid this error, you need to cast them to a string using str(). –  Mike T Oct 7 '11 at 1:56
Right. Thank you. Code fixed. –  Casey Oct 7 '11 at 2:25
I got my script to work properly! I utilized slicing and concatenation as you described, Casey. I also had to use getValue a few times for it to populate the field properly - as you can see in my initial question. Thank you for the replies! –  Patty Jula Oct 7 '11 at 18:18
add comment

If you want to round your coordinates and and have a bit more flexibility on lengths of character used for each coordinate, try this function:

def short_coord(x, y, len=4):
    div = 10**len
    return (str(int(round(x, 0) % div)).rjust(len, '0') + 
            str(int(round(y, 0) % div)).rjust(len, '0'))

You can test it out:

>>> short_coord(98765.432, 24680.5425)
>>> short_coord(95.432, 120.5425, len=3)

Note: I assume the inputs are floating point numbers, and the expected output is a string. Also, in case you are wondering, % is the handy modulo operator.

share|improve this answer
add comment

possibly change it to:

sourceRows = arcpy.UpdateCursor(inFeatures)  
for source in sourceRows:    
    source.fieldName3 == str(fieldname2)[1:5] + str(fieldname1)[1:5])
    del source

del sourceRows 

if source.setValue doesn't work, which occassionally it doesn't, for whatever reason...

share|improve this answer
Tis was meant as a comment to the answer above, but it wouldn't format, so I put it here. If you want me to delete it, please just make a comment and I will, please do not simply downmark for copying the answer, it's not meant as an answer. Cheers! –  Hairy Oct 7 '11 at 8:19
add comment

Your Answer


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.