ArcPy – Writing ArcPy Cursor Results to Text File

arcpytext;

I don't know how to write the result in a text file, can anyone help me?


import arcpy
rows = arcpy.SearchCursor("E:/Arcgis/lx.gdb/WuChangJieDaokou_onlyweight", "", "", "", "VEHICLEID A; TIME A") 
currentState = "" 
f=open('C:/Users/GWJ/Desktop/111.txt','w')
for row in rows: 
   currentState = row.VEHICLEID
   f.write(row.VEHICLEID,row.NEAR_X,row.NEAR_Y,row.TIME)

Runtime error
Traceback (most recent call last):
File "", line 7, in
TypeError: function takes exactly 1 argument (4 given)

Runtime error
Traceback (most recent call last):
File "", line 7, in
TypeError: expected a character buffer object

Best Answer

Are you trying to write multiple lines to the file or multiple values in the same line? Take into account that the f.write() method takes only one argument which is the value to be written in a specific line. Using the f.writelines() method allows you to specify a list of values to be written in a specific number of lines (depends on the size of the listt you pass to the method)

You could try using the following line if you want the values to be in the same line,

f.write("{0}, {1}, {2}, {3}".format(row.VEHICLEID,row.NEAR_X,row.NEAR_Y,row.TIME))

or the following line if you want them to be in separte lines,

f.writelines([row.VEHICLEID,row.NEAR_X,row.NEAR_Y,row.TIME])

Related Question