Select to view content in your preferred language

ModelBuilder: export attributes to a text file

1008
5
11-27-2012 06:28 AM
MaraKaminowitz
Frequent Contributor
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.  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.

Is there another option to export an attribute table to plain text?
Tags (2)
0 Kudos
5 Replies
DarrenWiens2
MVP Alum
I have no idea how to do this in ModelBuilder, but since this is the Python  forum here is a Python answer:
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:
    string = row.getValue("textfieldname") #change field
    outputTxtFile.write(string + '\n')
0 Kudos
MaraKaminowitz
Frequent Contributor
Thank you!  I can probably add this as a tool to ModelBuilder. if not I still have a working conversion step at the end.

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?
0 Kudos
curtvprice
MVP Alum

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?


Looks like the file buffer didn't get written to disk.

Did you close the file?
outputTxtFile.close()


If that didn't work you may want to try this:

outputTxtFile.flush()
outputTxtFile.close()


You don't need an external script, you could do this with the Calculate Value tool. Set up the input features as a precondition.

Expression:
writeFile(r"%Input Features%","FTEXT",r"c:\outfolder\outFile.txt")

Code Block:
import arcpy

def writeFile(inTable,Field,outFile):
  f = open(outFile, 'w')
  rows = arcpy.SearchCursor(inTable)
  for row in rows:
    f.write(str(row.getValue(Field)) + "\n")
  f.close()


Output type: File
0 Kudos
MaraKaminowitz
Frequent Contributor
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!

I did not know that you could do that in Calculate Value, I will keep that in mind. 

Thanks!
0 Kudos
curtvprice
MVP Alum
I found that when I opened it up and ran it as a tool, this problem went away.


This because when the script is run as a tool the buffer got flushed in the script cleanup when the script completes.

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.)
0 Kudos