I have a table in a geodatabase.
It looks like this:
id table_na
0 1 ER_ASTENOT_ASTOTA
1 2 INT_ASTENOT_ASTOTA
2 3 ER_ASTENOT_ASTTOM
It has more columns though.
Can you tell me how to read the column table_na line by line?
Example, reading by a txt:
with open(input()) as txtfile: #insert directory of txt
x = txtfile.readlines()
But I have to read from this specific column only.
Also tried this (with cursor):
env.workspace = r"C:\Users\user\Desktop\05052\MyProj.gdb"
datasetList = arcpy.ListTables("*")
for dataset in datasetList:
with arcpy.da.SearchCursor(dataset, "*") as cur:
for row in cur:
print row
But gives all that is in that table in one line.
Answer
You're very close. If you want to access just a single column, you can pass in the column name and then access in the cursor using the position within the row. (You can also request multiple fields in this manner; just add the field name into the list within da.SearchCursor.) Your code, modified, below:
env.workspace = r"C:\Users\user\Desktop\05052\MyProj.gdb"
datasetList = arcpy.ListTables("*")
for dataset in datasetList:
with arcpy.da.SearchCursor(dataset, ["table_na"]) as cur:
for row in cur:
print row[0]
Another example with more than one field:
for dataset in datasetList:
with arcpy.da.SearchCursor(dataset, ["mytestfield1", "mytestfield2", "table_na"]) as cur:
for row in cur:
print row[2] #retrieve table_na
No comments:
Post a Comment