AnsweredAssumed Answered

How to create a new field with a combination based on another field with arcpy?

Question asked by JuttaSchiller on Sep 23, 2014
Latest reply on Oct 7, 2014 by JuttaSchiller

I like to create a new txt which has two columns: Id and GRIDCODE.

This is the original Table:

 

table_frage.PNG

 

The Id column of the new txt should have just unique values. I’m able to do this with the following code. But the GRIDCODE -column of the new txt should have the possible combinations of one equal Id, for example:

The new Id field 47973 should have the new GRIDCODE field 7, 10

or the new Id field 47990 should have the new GRIDCODE field 4, 7.

To clarify the question I build an example-table with excel, this should be the result I like to write in the new txt. Do I have to work with an update cursor and if, else? What about the comma, how is it possible to use them without getting a new column but the combination in a column?

 

I would be very thankful to get a tip how I can go on with my code.

 

result_frage.PNG

 

import arcpy

import os

from arcpy import env

 

env.overwriteOutput = True

env.workspace = r"D:\Users\ju\ers\cities_UA\resultfolder"

 

DataDict = {}

inputshp = r"D:\Users\ju\ers\cities_UA\resultfolder\innsbruckauswahl.shp"

outputfile = r"D:\Users\ju\ers\cities_UA\resultfolder\inns_test.txt"

f = open (outputfile, 'w')

f.write ("ID,Gridcode,\n")

f.close ()

 

#f = open (outputfile, 'a')

 

ID=[row[0] for row in arcpy.da.SearchCursor(inputshp, ["Id", "GRIDCODE"])]

#GRIDCODE = [row[0] for row in arcpy.da.SearchCursor(inputshp, ["GRIDCODE"])]

uniqueID = set(ID)

for ID in uniqueID:

    print ID

 

# f.write((str(ID)) + "," + (str(GRIDCODE))+ "\n")

#f.close()

Outcomes