I want to calculate values within a field for a table (tableE) based on values of the field of another table (tableS) and within a loop:

Basically I create two lists of tables: table_EMISS and table_SURF and make the calculations for one table of each list giving certain matching conditions.

Finally I want to match the result table to a raster and create a new raster based on values of the calculated field of the table.

Here is my attempt so far:

#total emissions per country:
path = r"G:\new_scenario\new_table"
table_EMISS = []
for root, dirs, files in os.walk(path):
    arcpy.env.workspace = root
    files = arcpy.ListTables("emiss*")
    for fichero in files:
        table_EMISS.append(os.path.join(root, fichero))
table_SURF = []
for root, dirs, files in os.walk(path):
    arcpy.env.workspace = root
    files = arcpy.ListTables("total*")
    for fichero in files:
        (nombreFichero, extension) = os.path.splitext(fichero)
        table_SURF.append(os.path.join(root, fichero))
countries_RAS = "G:\\model_run\\countries"

#calculate one raster for each land use with calculated values of emissions
for tabE in table_EMISS:
    for tabS in table_SURF:
        if tabE[-6:-4] == tabS[-10:-8]: #join only tables with the same conditions
            arcpy.JoinField_management (tabE, "cou_abb", tabS, "cou_abb", "SUM")
            arcpy.AddField_management (tabE, "emiss_" + tabE[-6:-4], "DOUBLE")
            arcpy.CalculateField_management(tabE, "emiss_" + tabE[-6:-4], "!" + tabE [-12:-4] + ".Emissions!" * 1000000 / "!" + "!"+ tabE [-12:-4] + ".SUM!", "PYTHON", "#")
            arcpy.JoinField_management (countries_RAS, "cou_abb", tabE, "!" + tabE + "." + "cou_abb", tabE[-6:-4] + "2015")
            outLook = Lookup(countries_RAS, "!" + tabE [-12:-4] + "." + tabE[-6:-4] + "!")
            outLook.save(path + "\\emiss_" + data[3:] + ras [-8:-4])
            arcpy.RemoveJoin_management(countries_RAS, tabE)

I get stuck in the calculation of the new field getting this error:

TypeError: unsupported operand type(s) for /: 'str' and 'str'

I don't know if I'm not using the right expression to call fields: i.e.

"!" + tabE [-12:-4] + ".Emissions!"

refers to the name of the table (deleting the path and the extension of the file)

  • 2
    You've got a great deal of extraneous information in this question, yet it's missing necessary details (the exact contents of the expression and the types of the fields). If you construct a code snippet that isolates the problem, and search on the error message, you might find the problem resolves itself.
    – Vince
    Commented Oct 16, 2017 at 10:43
  • I agree with Vince. Try to focus on main issue without giving us "extra" info we don't need. Commented Oct 16, 2017 at 19:16

1 Answer 1


Thanks for your suggestions Vince. I managed to find the solution myself, the problem was that the multiplication was getting as inputs a string and a number, so I just adjusted everything to be read as a string:

arcpy.CalculateField_management(tabE, "emiss_" + tabE[-6:-4], "!" + "Emissions!" + "* 1000000 / !SUM!", "PYTHON", "#")

and this way everything run fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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