I like to create a new txt which has two columns: Id and GRIDCODE. This is the original Table:
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.
import arcpy
from arcpy import env
env.overwriteOutput = True
env.workspace = r"D:\Users\jul\ers\cities_UA\resultfolder"
inputshp = r"D:\Users\jul\ers\cities_UA\resultfolder\at003l_linz_result.shp"
outputfile = r"D:\Users\jul\ers\cities_UA\resultfolder\linz_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"])]
uniqueID = set(ID)
for ID in uniqueID:
print ID
f.write((str(ID)) + "," + "\n")
f.close() #working
Grindcode = [row[0] for row in arcpy.da.SearchCursor(inputshp, ["GRIDCODE"])]
???
Sorry, but I think I’m not able to write the code, what is wrong.
import arcpy
import os
from arcpy import env
env.overwriteOutput = True
env.workspace = r"D:\Users\julia\erste_aufg\cities_UA\resultfolder"
DataDict = {}
inputshp = r"D:\Users\julia\erste_aufg\cities_UA\resultfolder\innsbruckauswahl.shp"
outputfile = r"D:\Users\julia\erste_aufg\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 = [row[0] for row in arcpy.da.SearchCursor(inputshp, ["GRIDCODE"])]
uniqueID = set(ID)
GRIDCODE = set(GRIDCODE)
for ID in uniqueID:
print ID
if ID in DataDict:
DataDict[ID].append(GRIDCODE)
else:
DataDict[ID] = [GRIDCODE]
print DataDict
It is looking like this at the moment: