Writing for loop In Python using ArcMap, a naming issue

1825
2
Jump to solution
12-21-2019 09:02 PM
mohamednajar
New Contributor

In the script I'm running, each time the process runs, the output will have the same name as the output from the previous run of the process, and the previous output will be overwritten. how I can avoid overwriting previous outputs in successive iterations by appending the name of the output followed by the number of iteration, which will give each output a unique name that indicates its position in the list of inputs. I tried using the %i% system variable with in-line variable substitution as its shown in the script, but it seems that %i% just working within the Arcmap model-builder.

Here I'm sharing the script with you, and the list I used.

import arcpy
with open('D:\\referencedata\\fishnet\\listtest.txt', 'r') as coordinate:
   for line in coordinate:
      # Local variables:
      Input = "D:\\referencedata\\Menderes_1KM_MPI-RF_19710101-20051030.nc"
      NetCDFLayerName = "Menderes_1KM_MPI-RF_19710101%i%"
      Output__xls = "D:\\referencedata\\fishnet\\table\\Menderes_1KM_MPI-RF_19710101_%i%.xls"
      # Process: Make NetCDF Table View
      arcpy.MakeNetCDFTableView_md(Input, "pr;tas;tasmax;tasmin;sfcWind;hurs;rsns", NetCDFLayerName, "time", line,       "BY_VALUE")
      # Process: Table To Excel
      arcpy.TableToExcel_conversion(NetCDFLayerName, Output__xls, "ALIAS", "CODE")

the list looks like this;

jx -53000;iy -33000
jx -52000;iy -33000
jx -51000;iy -33000
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

reformatted to make it easier to read and reduce redundancies.  an 'enumerator' enables you to use a counter which automagically increments or you need a counter for the %i% thing.

Whether it works is another thing

import arcpy
Input = "D:\\referencedata\\Menderes_1KM_MPI-RF_19710101-20051030.nc"
out_folder = "D:\\referencedata\\fishnet\\table"
with open('D:\\referencedata\\fishnet\\listtest.txt', 'r') as coordinate:
    for i, line in enumerate(coordinate):
        # Local variables:        
        NetCDFLayerName = "Menderes_1KM_MPI-RF_19710101{0}".format(i)
        name = "{0}\\Menderes_1KM_MPI-RF_19710101_{1}.xls"
        Output__xls = name.format(out_folder, i)
        # Process: Make NetCDF Table View
        arcpy.MakeNetCDFTableView_md(Input, 
                                     "pr;tas;tasmax;tasmin;sfcWind;hurs;rsns",
                                     NetCDFLayerName,
                                     "time",
                                     line,
                                     "BY_VALUE")
        # Process: Table To Excel
        arcpy.TableToExcel_conversion(NetCDFLayerName,
                                      Output__xls,
                                      "ALIAS",
                                      "CODE")‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

2 Replies
DanPatterson_Retired
MVP Emeritus

reformatted to make it easier to read and reduce redundancies.  an 'enumerator' enables you to use a counter which automagically increments or you need a counter for the %i% thing.

Whether it works is another thing

import arcpy
Input = "D:\\referencedata\\Menderes_1KM_MPI-RF_19710101-20051030.nc"
out_folder = "D:\\referencedata\\fishnet\\table"
with open('D:\\referencedata\\fishnet\\listtest.txt', 'r') as coordinate:
    for i, line in enumerate(coordinate):
        # Local variables:        
        NetCDFLayerName = "Menderes_1KM_MPI-RF_19710101{0}".format(i)
        name = "{0}\\Menderes_1KM_MPI-RF_19710101_{1}.xls"
        Output__xls = name.format(out_folder, i)
        # Process: Make NetCDF Table View
        arcpy.MakeNetCDFTableView_md(Input, 
                                     "pr;tas;tasmax;tasmin;sfcWind;hurs;rsns",
                                     NetCDFLayerName,
                                     "time",
                                     line,
                                     "BY_VALUE")
        # Process: Table To Excel
        arcpy.TableToExcel_conversion(NetCDFLayerName,
                                      Output__xls,
                                      "ALIAS",
                                      "CODE")‍‍‍‍‍‍‍‍‍‍‍‍
mohamednajar
New Contributor

Thank you Dan Patterson‌, its work  

I tried this approach and worked, too.

import arcpy
i = 0
with open('D:\\referencedata\\fishnet\\listtest.txt', 'r') as L:
    for line in L:
		# Local variables:
		Input = "D:\\referencedata\\Menderes_1KM_MPI-RF_19710101-20051030.nc"
		NetCDFLayerName = "Menderes_1KM_MPI-RF_19710101_" + str(i)
		Output__xls = "D:\\referencedata\\fishnet\\table\\Menderes_1KM_MPI-RF_19710101_" + str(i) + ".xls"
		# Process: Make NetCDF Table View
		arcpy.MakeNetCDFTableView_md(Input, "pr;tas;tasmax;tasmin;sfcWind;hurs;rsns", NetCDFLayerName, "time", line, "BY_VALUE")
		# Process: Table To Excel
		arcpy.TableToExcel_conversion(NetCDFLayerName, Output__xls, "ALIAS", "CODE")
		i = i + 1

 

0 Kudos