1

I like to create a new txt which has two columns: Id and GRIDCODE. This is the original Table:

enter image description here

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.

enter image description here

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:

enter image description here

3

Why not use a dictionary when reading your input data. The key is the ID, the values are the GRIDCODEs. Something like :

DataDict = {}
# open cursor etc

if ID in DataDict:
    DataDict[ID].append(GRIDCODE)
else:
    DataDict[ID] = [GRIDCODE]

Then for each ID, you can process the associated list of GRIDCODEs using set() as you have done.

  • Thanks a lot, I will try it. (Having now experience in using dictionary it will take time…) – Nora Sep 22 '14 at 11:32
  • @ Neil Ayres: Sorry, but I think I’m not able to write the code, what is wrong. My current code is at the of the question. – Nora Sep 23 '14 at 11:57
1

Sorry Nora for responding so late... Instead of doing the 2 list comprehensions separately, you have to associate each ID with its list of GRIDCODES. So

for row in arcpy.da.SearchCursor(inputshp, ["ID", "GRIDCODE"]):
    ID = row[0]
    GC = row[1]
    if ID in DataDict:

        DataDict[ID].append(GC)
    else:
        DataDict[ID] = [GC]

# then run through the dictionary again to make each list of gridcodes unique
for k, v in DataDict.iteritems():
    GCUnique = list(set(v))
    # reset the dictionary
    DataDict[k] = GCUnique

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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