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)