Write Multiple Attribute Tables to Single CSV File

2920
8
Jump to solution
01-31-2021 01:35 PM
WilliamCole
Occasional Contributor

The following is supposed to write the partial content of four Attribute Tables [AT] to a single CSV file.

2021-01-31_16-14-52.png

While it finishes “successfully”, it only writes the last of the four tables to the specified CSV file [MyOutput8.csv].

2021-01-31_16-22-17.png

I had though that the statement arcpy.env.overwriteOutput=True on line 19 would address that but clearly I’m wrong.

 

2021-01-31_16-14-52.png 

The CSV output looks like this, which is correct, except for the missing three AT’s that should precede it.

 

 2021-01-31_16-22-17.png

Advice, please. Thank you so much.

 

Tags (1)
0 Kudos
3 Solutions

Accepted Solutions
DavidPike
MVP Frequent Contributor

in your with open statement, you're opening it in write mode, whereby the file is recreated with no data if it already exists.  In append mode 'a' it should append to the file if it already exists, or create it initially if it does not.  N.B overwriteOutput is for arcpy outputs only.

I've not gone through the rest of the code, but this may be a good start.

#line26
with open(outputCSV, "a") as csv file:

View solution in original post

by Anonymous User
Not applicable

I agree with what has been said about the 'a' and would add a tip of throwing in some arcpy.AddMessage() statements so you can see what is being selected/ written.  I'd do this so I can watch for fc's that are being skipped due to the ListFeatureClasses filter not catching them as intended, such as misspelled files that aren't that obvious to spot. For example, Route_Homebound_03 and Route_Homebound_O3.

View solution in original post

WilliamCole
Occasional Contributor

This solved the problem.  Bless you and thanks so very much for the advice!

View solution in original post

8 Replies
DavidPike
MVP Frequent Contributor

in your with open statement, you're opening it in write mode, whereby the file is recreated with no data if it already exists.  In append mode 'a' it should append to the file if it already exists, or create it initially if it does not.  N.B overwriteOutput is for arcpy outputs only.

I've not gone through the rest of the code, but this may be a good start.

#line26
with open(outputCSV, "a") as csv file:
WilliamCole
Occasional Contributor

This solved the problem.  Bless you and thanks so very much for the advice!

by Anonymous User
Not applicable

Hi @WilliamCole ,

The issue you're seeing here is likely to be to do with open(outputCSV,"w"). As you're probably aware the "w" in this context opens the filename in the variable outputCSV for writing - however a side effect of using the "w" is that it re-creates the file, removing any existing content in the file before writing your output. So the current behaviour of your script will be writing each attribute table to the file, but you're only see the last one because it's clearing the previous output each time we reopen the file. 

There's a couple different ways we could work around this - one would be to move the file opening outside of the loop. That way we could open the file once and write each attribute table to the file. 

 

with open(outputCSV, "w") as csvfile:
    for value in fclist:
        ...

 

 

The other way to approach it would be to change the  open(outputCSV, "w") to  open(outputCSV, "a") - the "a" opens the file for appending data, which just means it won't clear the existing contents of the file each time it opens the file. 

Hope that helps!

 

WilliamCole
Occasional Contributor

Thanks very much; that did the job!

0 Kudos
Luke_Pinner
MVP Regular Contributor
0 Kudos
WilliamCole
Occasional Contributor
Sorry, will do that next time. Regards, Bill Cole
0 Kudos
by Anonymous User
Not applicable

I agree with what has been said about the 'a' and would add a tip of throwing in some arcpy.AddMessage() statements so you can see what is being selected/ written.  I'd do this so I can watch for fc's that are being skipped due to the ListFeatureClasses filter not catching them as intended, such as misspelled files that aren't that obvious to spot. For example, Route_Homebound_03 and Route_Homebound_O3.

WilliamCole
Occasional Contributor

Thanks to everyone for you help.  i don't know how to thank you all.  This problem has been around for awhile and - I'm sure you've figured this out - am still learning ArcPy.  Be well.  Stay safe.