Iterate Through Attribute Tables to Create Concatenated .csv File

133
4
Jump to solution
01-11-2021 12:58 PM
Labels (2)
WilliamCole
Occasional Contributor

This is for ArcGIS 10.8.1 and Python 2.7

 

Using Blake Terhune’s solution [thank you, Blake] shown in the ESRI Community article Export Attribute Table to .csv as a basis to create a .csv file from the selected contents of an attribute table, I need to go one step further and modify that script so that it will cycle through a defined number of Attribute Tables within a defined .gdb. [Leaving the changes out, the script works fine for a single FC.]

 

To do that I made these changes, which, it appears, to have been wrong. Here’s the modified script with the modifications highlighted in yellow:

 

Modified CSV ScriptModified CSV Script

 

1. I added line 12 which is intended to create a variable that extracts all FC’s that end with “03”.

2. Then modified line 15 so that that variable defined the array of input tables.

3. Finally added a for-loop as line 20 to cycle through those tables defined by line 15, outputting a – hopefully – concatenated .csv file containing all of the Attribute Table entries.

 

This didn’t work as shown by this run-time result:

 

ArcPy ErrorArcPy Error 

I checked to see if all the Fields defined in line 25 were spelled correctly – as I gotten a similar error when there was a problem with the field list – but they were all spelled and capitalized correctly. I would be grateful for some guidance on where I have gone wrong, please.

 

Thanks so much,

 

Reply
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Regular Contributor

That would probably cause an unexpected indent error, you can do stuff on one line but not if you then indent it, and I wouldn't make it a habit even so as it's probably bad styling.

I'd also recommend adding the code via the syntax highlighter on the forum. click on the ellipsis '...' on the right hand side of the options, then insert/edit code sample - </>

this makes it much easier to read and adjust supplied code.

 

what i meant was:

for value in fclist:
    inputTable = os.path.join ....
    with open(outputCSV, 'w') as csvfile:
        csvwriter = .....

View solution in original post

4 Replies
DavidPike
MVP Regular Contributor

Without dissecting it, it seems to be nearly there.

I believe you have a bit of a mistake in trying to specify the path to the input table a bit too early on line 15.  What you're doing is trying to join the workspace to a list.

It could make sense if os.path.join was set up to take a list as an argument, but unfortunately it doesn't.

I would get rid of that and insert this after line 20:

 

for value in fclist:
    inputTable = os.path.join(workingGDB, value)
    

 effectively, during each iteration, the workingGDB path is joined to the FC name for each loop.

WilliamCole
Occasional Contributor

Mr. Pike, thank you very much for your input.  Obviously I still have much to learn about the Python/AcrPy world. 

Just to make sure that I understood your suggestions, below is a modified script, hopefully reflecting the changes you offered.  Please let me know if I have made yet another mistake.

2021-01-12_9-58-26.png

Reply
0 Kudos
DavidPike
MVP Regular Contributor

That would probably cause an unexpected indent error, you can do stuff on one line but not if you then indent it, and I wouldn't make it a habit even so as it's probably bad styling.

I'd also recommend adding the code via the syntax highlighter on the forum. click on the ellipsis '...' on the right hand side of the options, then insert/edit code sample - </>

this makes it much easier to read and adjust supplied code.

 

what i meant was:

for value in fclist:
    inputTable = os.path.join ....
    with open(outputCSV, 'w') as csvfile:
        csvwriter = .....

View solution in original post

WilliamCole
Occasional Contributor

Thank you again.  I should have picked up on the need to make a new line but what do I know.  Very much appreciate your patience and help.  Be well.  Bill Cole