<?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: ModelBuilder: export attributes to a text file in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27920#M2137</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have no idea how to do this in ModelBuilder, but since this is the Python&amp;nbsp; forum here is a Python answer:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

inputFeatureClass = r"H:\GIS_Data\TEMP.gdb\points" #change to your input file path
outputTxtFile = open(r"H:\GIS_Data\outputfile.txt", 'w') #change to your text file

rows = arcpy.SearchCursor(inputFeatureClass, "", "", "textfieldname") #change field

for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; string = row.getValue("textfieldname") #change field
&amp;nbsp;&amp;nbsp;&amp;nbsp; outputTxtFile.write(string + '\n')&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 21:07:39 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2021-12-10T21:07:39Z</dc:date>
    <item>
      <title>ModelBuilder: export attributes to a text file</title>
      <link>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27919#M2136</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have an attribute table with a single field - a long text string that includes spaces, commas, and letters, etc. I would like to export this data to a plain text file using ModelBuilder. I just want to end up with a list with one field value per line and it must retain the spaces at the end.&amp;nbsp; I have tried the Python "Export to ASCII" tool but it is meant to deal with simple numeric fields and treats the spaces in my text like delimiters. It does not matter how I set up the export tool, it still happens. Adding quotes to the field doesn't help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is there another option to export an attribute table to plain text?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 27 Nov 2012 14:28:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27919#M2136</guid>
      <dc:creator>MaraKaminowitz</dc:creator>
      <dc:date>2012-11-27T14:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: ModelBuilder: export attributes to a text file</title>
      <link>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27920#M2137</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have no idea how to do this in ModelBuilder, but since this is the Python&amp;nbsp; forum here is a Python answer:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

inputFeatureClass = r"H:\GIS_Data\TEMP.gdb\points" #change to your input file path
outputTxtFile = open(r"H:\GIS_Data\outputfile.txt", 'w') #change to your text file

rows = arcpy.SearchCursor(inputFeatureClass, "", "", "textfieldname") #change field

for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; string = row.getValue("textfieldname") #change field
&amp;nbsp;&amp;nbsp;&amp;nbsp; outputTxtFile.write(string + '\n')&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:07:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27920#M2137</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-10T21:07:39Z</dc:date>
    </item>
    <item>
      <title>Re: ModelBuilder: export attributes to a text file</title>
      <link>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27921#M2138</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you!&amp;nbsp; I can probably add this as a tool to ModelBuilder. if not I still have a working conversion step at the end.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One oddity - the first time I run it, I don't get results. I click run a second time (using IDLE) and it works. Any idea why?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 27 Nov 2012 16:54:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27921#M2138</guid>
      <dc:creator>MaraKaminowitz</dc:creator>
      <dc:date>2012-11-27T16:54:53Z</dc:date>
    </item>
    <item>
      <title>Re: ModelBuilder: export attributes to a text file</title>
      <link>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27922#M2139</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;One oddity - the first time I run it, I don't get results. I click run a second time (using IDLE) and it works. Any idea why?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Looks like the file buffer didn't get written to disk.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Did you close the file?&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;outputTxtFile.close()&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;that&lt;/SPAN&gt;&lt;SPAN&gt; didn't work you may want to try this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;outputTxtFile.flush()
outputTxtFile.close()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You don't need an external script, you could do this with the Calculate Value tool. Set up the input features as a precondition.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression:&lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;writeFile(r"%Input Features%","FTEXT",r"c:\outfolder\outFile.txt")&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Code Block:&lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy

def writeFile(inTable,Field,outFile):
&amp;nbsp; f = open(outFile, 'w')
&amp;nbsp; rows = arcpy.SearchCursor(inTable)
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.write(str(row.getValue(Field)) + "\n")
&amp;nbsp; f.close()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Output type: File&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:07:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27922#M2139</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-10T21:07:42Z</dc:date>
    </item>
    <item>
      <title>Re: ModelBuilder: export attributes to a text file</title>
      <link>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27923#M2140</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I found that when I opened it up and ran it as a tool, this problem went away. I assume the tool functions are handling the buffer. I was able to modify the script to take parameters in my model - don't ask me how since the basics of writing to a file eluded me!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I did not know that you could do that in Calculate Value, I will keep that in mind.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Nov 2012 11:47:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27923#M2140</guid>
      <dc:creator>MaraKaminowitz</dc:creator>
      <dc:date>2012-11-28T11:47:02Z</dc:date>
    </item>
    <item>
      <title>Re: ModelBuilder: export attributes to a text file</title>
      <link>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27924#M2141</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I found that when I opened it up and ran it as a tool, this problem went away. &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This because when the script is run as a tool the buffer got flushed in the script cleanup when the script completes.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In IDLE, you're running things from an interactive Python root process that does not go away when you run the code in a window. (This is so you can do debugging.)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Nov 2012 15:20:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modelbuilder-export-attributes-to-a-text-file/m-p/27924#M2141</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2012-11-28T15:20:22Z</dc:date>
    </item>
  </channel>
</rss>

