The following is supposed to write the partial content of four Attribute Tables [AT] to a single CSV file.
While it finishes “successfully”, it only writes the last of the four tables to the specified CSV file [MyOutput8.csv].
I had though that the statement arcpy.env.overwriteOutput=True on line 19 would address that but clearly I’m wrong.
The CSV output looks like this, which is correct, except for the missing three AT’s that should precede it.
Advice, please. Thank you so much.
Solved! Go to Solution.
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:
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.
This solved the problem. Bless you and thanks so very much for the advice!
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:
This solved the problem. Bless you and thanks so very much for the advice!
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!
Thanks very much; that did the job!
Please can you post code, not screenshots of code. https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633
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.
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.