4

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

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Apr 5, 2018 at 14:16
1
  • Rule #1 of ArcGIS Cursors: Do not use arcpy.SearchCursor. Instead use DataAccess cursors (arcpy.da.SearchCursor). Old-style cursors are deprecated, slow, and anti-Pythonic. Learn to use DA cursors now. Rows will become lists and life will be wonderful. Commented Apr 5, 2018 at 21:46

3 Answers 3

4

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])

answered Apr 5, 2018 at 14:49
0
2

Looks like you may have forgotten to use "open" instead of "file" in your opening of the file:

*f=file('C:/Users/GWJ/Desktop/111.txt','w')

should be:

f=open('C:/Users/GWJ/Desktop/111.txt','w')

but best practice is to use a "with" so you don't need to worry about closing your file when done/error/hang/etc:

with open('C:/Users/GWJ/Desktop/111.txt','w') as f:

http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

answered Apr 5, 2018 at 14:29
5
  • But it reported the same error after modification Commented Apr 5, 2018 at 14:34
  • What exactly are line 9 and 10 in your code? Commented Apr 5, 2018 at 14:37
  • 1
    As an aside, I would use a "with" statement with your search cursor as well: with arcpy.SearchCursor(whatever) as rows: Commented Apr 5, 2018 at 14:38
  • +1 for using the with statement for best practice Commented Apr 5, 2018 at 14:46
  • I have already updated the newest error in my question.If I change the parameter to one,It will also report an error as above. Commented Apr 5, 2018 at 14:48
2

I would urge you to avoid using the deprecated, non-data access module ArcGIS cursor. The following approach generates a list of rows as tuples using a list comprehension then writes the lines to a text file using writelines:

import arcpy
fc = r'C:\path\to\your\geodatabase.gdb\featureclass'
txt = r'C:\path\to\your\txt_file.txt'
lines = [row for row in arcpy.da.SearchCursor(fc, ("VEHICLEID", "NEAR_X", "NEAR_Y", "TIME"))]
with open(txt, 'w') as txtfile:
 txtfile.writelines(str(i).strip("()") + "\n" for i in lines)
answered Apr 5, 2018 at 18:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.