Problem with calculate field in python

4840
9
Jump to solution
01-27-2015 12:43 AM
JohannesBierer
Regular Contributor

Somehow I can't manage to get this to work:

The goal is to write a path into a field combined with the attribute Object_ID

import arcpy, os
Fgdb = r"R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster\RasterCatalog.gdb"
arcpy.env.workspace = r"R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster\RasterCatalog.gdb"
# Ausgabepfad = (r"R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster").strip()
Ausgabepfad = r"R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster"
ext = ".jpg"
# arcpy.AddField_management(Ausgabepfad + "\Orthos_Nummern.shp", "IMAGE", "TEXT", 100) # , "", "", "refcode", "NULLABLE", "REQUIRED")
# print (Ausgabepfad + "\Orthos_Nummern.shp")
# print ("\"R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster\"" + ext)
# print (Ausgabepfad + "\\Orthos_Nummern.shp")
rows = arcpy.UpdateCursor(Ausgabepfad + "\Orthos_Nummern.shp")  
for row in rows:
    # yield ('"R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster\"' + '(!OBJECT_ID!)' + ext).strip()
    # print (('"R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster\ + '(!OBJECT_ID!)' + 'ext').strip('utf-8'))
    print (('R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster\' + '(!OBJECT_ID!)' + 'ext').strip('utf-8'))
    # arcpy.CalculateField_management(Ausgabepfad + "\\Orthos_Nummern.shp", "IMAGE", 
('"R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster\"' + '(!OBJECT_ID!)' + ext).strip('utf-8'), "PYTHON")

???

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

To create a unique list of values from a field inside a cursor would repeat that for every feature, while the list remains the same.

If I understand correctly, you want to construct a path (string) and store that in a field. You could use this:

import arcpy, os
Ausgabepfad = r"R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster"
fgdb = "RasterCatalog.gdb" # you're not using this
arcpy.env.workspace = os.path.join(Ausgabepfad, fgdb) # you're not using this
ext = ".jpg"
fc = os.path.join(Ausgabepfad, "Orthos_Nummern.shp")
fld_oid = "OBJECT_ID" # or: arcpy.Describe(fc).OIDFieldName
fld_out = "YourOutputFieldName" # should exist in shapefile

rows = arcpy.UpdateCursor(fc)
for row in rows:
    path = os.path.join(Ausgabepfad, "{0}{1}".format(row.getValue(fld_oid), ext))
    row.setValue(fld_out, path)
    curs.updateRow(row)

# or if you have a version >= 10.1 SP1, you could use the arc.da.UpdateCursor
flds = (fld_oid, fld_out)
with arcpy.da.UpdateCursor(fc, flds) as curs:
    for row in curs:
        row[1] = os.path.join(Ausgabepfad, "{0}{1}".format(row[0], ext))
        curs.updateRow(row)

View solution in original post

0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

why is  '(!OBJECT_ID!)' in round brackets? and if it is numeric, it needs to be converted to a string ... str( ... )

0 Kudos
Luis_ÁngelPascual_Camino
New Contributor III

Hi Johannes.

Why don't you try to write every backslash double? I mean that every special character need to be scaped, and the scape character is the backslash.

For example:

print (('R:\\Karto\Bierer2014\\Datenabgabe\\20150113_Brandt\\raster\\' + '(!OBJECT_ID!)' + 'ext').strip('utf-8'))

I hope it helps.

Luis

Luis_ÁngelPascual_Camino
New Contributor III

...I meaned "escaped"... sorry.

0 Kudos
JohannesBierer
Regular Contributor

Thanks for help. If I try this:

print (('R:\\Karto\Bierer2014\\Datenabgabe\\20150113_Brandt\\raster\\' + '(!OBJECT_ID!)' + ext).strip('utf-8'))

I get this message:

AttributeError: 'str' object has no attribute 'OBJECT_ID'

I do understand that I haven't told python properly that it should take attribute of the field Objec_ID of the shape.

How to do that.

Also tried with no sucess:

for row in rows:

    unique_values = set(row.getValue(Field) for row in iter(arcpy.SearchCursor(Path).next, None))

0 Kudos
XanderBakker
Esri Esteemed Contributor

To create a unique list of values from a field inside a cursor would repeat that for every feature, while the list remains the same.

If I understand correctly, you want to construct a path (string) and store that in a field. You could use this:

import arcpy, os
Ausgabepfad = r"R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster"
fgdb = "RasterCatalog.gdb" # you're not using this
arcpy.env.workspace = os.path.join(Ausgabepfad, fgdb) # you're not using this
ext = ".jpg"
fc = os.path.join(Ausgabepfad, "Orthos_Nummern.shp")
fld_oid = "OBJECT_ID" # or: arcpy.Describe(fc).OIDFieldName
fld_out = "YourOutputFieldName" # should exist in shapefile

rows = arcpy.UpdateCursor(fc)
for row in rows:
    path = os.path.join(Ausgabepfad, "{0}{1}".format(row.getValue(fld_oid), ext))
    row.setValue(fld_out, path)
    curs.updateRow(row)

# or if you have a version >= 10.1 SP1, you could use the arc.da.UpdateCursor
flds = (fld_oid, fld_out)
with arcpy.da.UpdateCursor(fc, flds) as curs:
    for row in curs:
        row[1] = os.path.join(Ausgabepfad, "{0}{1}".format(row[0], ext))
        curs.updateRow(row)
0 Kudos
JohannesBierer
Regular Contributor

Look good Xander, unfortunaltely

path = os.path.join(Ausgabepfad, "{0}{1}".format(row.getValue(fld_oid), ext))

leads to that:

R:\Karto\Bierer2014\Datenabgabe\20150113_Brandt\raster\35355481.0.jpg

I would need it without .0  = xxx481.jpg ?

and row.getValue = TypeError: 'float' object is not subscriptable ?

0 Kudos
XanderBakker
Esri Esteemed Contributor

On line 12, you shoud convert the float to int to avoid the ".0":

path = os.path.join(Ausgabepfad, "{0}{1}".format(int(row.getValue(fld_oid)), ext))

The other error is a bit weird (maybe that´s why I don't use the old cursor anymore...) If you have 10.1 SP1 or higher switch to the arcpy.daUpdateCursor syntax. Which would be:

flds = (fld_oid, fld_out)
with arcpy.da.UpdateCursor(fc, flds) as curs:
    for row in curs:
        row[1] = os.path.join(Ausgabepfad, "{0}{1}".format(int(row[0]), ext))
        curs.updateRow(row)
0 Kudos
JohannesBierer
Regular Contributor

Unfortunately I can't use arcpy.da. 😞

Small mistake in your code:

Not:

curs.updateRow(row)

It's ?:

rows.updateRow(row)

Had only to make the value to integer:

fld_oid1 = int(row.getValue(fld_oid))

int_fld_oid1 = str(fld_oid1)

0 Kudos
XanderBakker
Esri Esteemed Contributor

On line 2 of the code you mention, I created a cursor object and named it "curs". You can call it "rows" or anything you like. That doesn't matter, it is the name you use for a variable (in this case a cursor object). So it is not an error, but a way I like to call the object/variable.

0 Kudos