<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Write Multiple Attribute Tables to Single CSV File in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1021961#M59720</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/77573"&gt;@WilliamCole&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with open(outputCSV, "w") as csvfile:
    for value in fclist:
        ...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The other way to approach it would be to change the&amp;nbsp; open(outputCSV, "w") to&amp;nbsp; 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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope that helps!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 31 Jan 2021 22:15:22 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2021-01-31T22:15:22Z</dc:date>
    <item>
      <title>Write Multiple Attribute Tables to Single CSV File</title>
      <link>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1021956#M59718</link>
      <description>&lt;P&gt;The following is supposed to write the partial content of four Attribute Tables [AT] to a single CSV file.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-01-31_16-14-52.png" style="width: 862px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/4991i3FE32BC227B667AC/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-01-31_16-14-52.png" alt="2021-01-31_16-14-52.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;While it finishes “successfully”, it only writes the last of the four tables to the specified CSV file [&lt;I&gt;MyOutput8.csv&lt;/I&gt;].&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-01-31_16-22-17.png" style="width: 729px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/4992iD5871984B3EECCCB/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-01-31_16-22-17.png" alt="2021-01-31_16-22-17.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I had though that the statement &lt;I&gt;arcpy.env.overwriteOutput=True&lt;/I&gt; on line 19 would address that but clearly I’m wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-01-31_16-14-52.png" style="width: 862px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/4989i2DC5B78E9AAA388C/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-01-31_16-14-52.png" alt="2021-01-31_16-14-52.png" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The CSV output looks like this, which is correct, &lt;U&gt;except&lt;/U&gt; for the missing three AT’s that should precede it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-01-31_16-22-17.png" style="width: 729px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/4990i07AE07CE78C4CC44/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-01-31_16-22-17.png" alt="2021-01-31_16-22-17.png" /&gt;&lt;/span&gt; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Advice, please. Thank you so much.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 21:35:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1021956#M59718</guid>
      <dc:creator>WilliamCole</dc:creator>
      <dc:date>2021-01-31T21:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: Write Multiple Attribute Tables to Single CSV File</title>
      <link>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1021960#M59719</link>
      <description>&lt;P&gt;in your with open statement, you're opening it in write mode, whereby the file is recreated with no data if it already exists.&amp;nbsp; In append mode 'a' it should append to the file if it already exists, or create it initially if it does not.&amp;nbsp; N.B overwriteOutput is for arcpy outputs only.&lt;/P&gt;&lt;P&gt;I've not gone through the rest of the code, but this may be a good start.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#line26
with open(outputCSV, "a") as csv file:&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 31 Jan 2021 22:11:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1021960#M59719</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-01-31T22:11:02Z</dc:date>
    </item>
    <item>
      <title>Re: Write Multiple Attribute Tables to Single CSV File</title>
      <link>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1021961#M59720</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/77573"&gt;@WilliamCole&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with open(outputCSV, "w") as csvfile:
    for value in fclist:
        ...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The other way to approach it would be to change the&amp;nbsp; open(outputCSV, "w") to&amp;nbsp; 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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope that helps!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 22:15:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1021961#M59720</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-01-31T22:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: Write Multiple Attribute Tables to Single CSV File</title>
      <link>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1021967#M59721</link>
      <description>&lt;P&gt;Please can you post code, not screenshots of code.&amp;nbsp;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 22:42:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1021967#M59721</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-01-31T22:42:56Z</dc:date>
    </item>
    <item>
      <title>Re: Write Multiple Attribute Tables to Single CSV File</title>
      <link>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1021988#M59722</link>
      <description>&lt;P&gt;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.&amp;nbsp; 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.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Feb 2021 13:14:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1021988#M59722</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-02-01T13:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: Write Multiple Attribute Tables to Single CSV File</title>
      <link>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1022064#M59726</link>
      <description>Sorry, will do that next time. Regards, Bill Cole&lt;BR /&gt;</description>
      <pubDate>Mon, 01 Feb 2021 13:30:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1022064#M59726</guid>
      <dc:creator>WilliamCole</dc:creator>
      <dc:date>2021-02-01T13:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: Write Multiple Attribute Tables to Single CSV File</title>
      <link>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1022070#M59728</link>
      <description>&lt;P&gt;This solved the problem.&amp;nbsp; Bless you and thanks so very much for the advice!&lt;/P&gt;</description>
      <pubDate>Mon, 01 Feb 2021 13:40:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1022070#M59728</guid>
      <dc:creator>WilliamCole</dc:creator>
      <dc:date>2021-02-01T13:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: Write Multiple Attribute Tables to Single CSV File</title>
      <link>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1022072#M59729</link>
      <description>&lt;P&gt;Thanks very much; that did the job!&lt;/P&gt;</description>
      <pubDate>Mon, 01 Feb 2021 13:43:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1022072#M59729</guid>
      <dc:creator>WilliamCole</dc:creator>
      <dc:date>2021-02-01T13:43:08Z</dc:date>
    </item>
    <item>
      <title>Re: Write Multiple Attribute Tables to Single CSV File</title>
      <link>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1022074#M59730</link>
      <description>&lt;P&gt;Thanks to everyone for you help.&amp;nbsp; i don't know how to thank you all.&amp;nbsp; This problem has been around for awhile and - I'm sure you've figured this out - am still learning ArcPy.&amp;nbsp; Be well.&amp;nbsp; Stay safe.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Feb 2021 13:46:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/write-multiple-attribute-tables-to-single-csv-file/m-p/1022074#M59730</guid>
      <dc:creator>WilliamCole</dc:creator>
      <dc:date>2021-02-01T13:46:06Z</dc:date>
    </item>
  </channel>
</rss>

