# Writing fields from point shapefile to text file using ArcPy? [closed]

I have the following script, trying to take a cities shapefile and create an exported text file that will list each feature in the text file with (I can't figure out where I'm going wrong):

CityName1, CoordX, CoordY

CityName2, CoordX, CoordY

import arcpy
arcpy.env.workspace = "C:\\Data"
citiesFile = "C:\\Data\\Cities.shp"
fields = ["SHAPE@XY"]
output = open("C:\\Data\\CO_Cities.csv", "w")
with open("C:\\Data\\CO_Cities.csv", "w") as COCitiesFile:
with arcpy.da.SearchCursor(citiesFile, "SHAPE@") as cursor:
point = row[0].getPart(0)
citiesFile.write("{}, {}\n".format(point.X, point.Y))


## closed as unclear what you're asking by Luke, whyzar, Jochen Schwarze, Mark Ireland, Andre SilvaOct 18 '18 at 15:43

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• The script is supposed to take a point shapefile and create a text file that lists each feature with its; Name, Coordx, and CoordY. Every feature should be listed on its own line – user129835 Oct 18 '18 at 2:43
• Right now, this does nothing. I'm trying to figure out where I'm going wrong and possibly get an example that would point me in the right direction. – user129835 Oct 18 '18 at 2:46
• Welcome to Geographic Information Systems! Please don't just say but it didn't work or I can't figure out where I'm going wrong, that will attract downvotes and close votes. Instead can you edit your question and say what actually did happen. Was there an application/software crash, was there an error message (if so what was the complete error message as text, not screenshot), did the program run but produce incorrect/unexpected results (if so what were they)? – Luke Oct 18 '18 at 6:54

You need to define the fields you are working with and use the proper tokens. Try the following (untested) approach:

import arcpy

shp = r'"C:\Data\Cities.shp"'
txt = r'C:\path\to\your\txt_file.txt'

# Extract the information from the shapefile
lines = [row for row in arcpy.da.SearchCursor(shp, ("CityName", "SHAPE@X", "SHAPE@Y"))]

# Write to text file
with open(txt, 'w') as txtfile:
txtfile.writelines(str(i).strip("()") + "\n" for i in lines)


I can see four problems with your script:

1. It looks like your script is attempting to write to the shapefile (citiesFile) instead of to the CSV file (COCitiesFile).

2. You need to delete your output = line. It is unused and perhaps prevents the next line and the write from working as desired, by trying to open the same file twice! (You can also delete your arcpy.env. line as it is unused).

3. You also need to include the city name field in your search cursor and your file writing.

4. Your row variable is undefined (are you sure your script produced no errors?). You should iterate through the search cursor using a for loop and can use a row variable for that.

The end result would look something like this:

import arcpy
citiesFile = "C:\\Data\\Cities.shp"
fields = ["SHAPE@XY", "CityName"]
with open("C:\\Data\\CO_Cities.csv", "w") as COCitiesFile:
with arcpy.da.SearchCursor(citiesFile, fields) as cur:
for row in cur:
COCitiesFile.write("{}, {}, {}\n".format(row[1], row[0][0], row[0]][1]))


@Aaron's answer also provides a good alternative method for achieving a similar result. I've tried to keep this answer as close to your original script's methodology, formatting and token selection as possible (in order to illustrate the errors, and the fixes for each error).

Another way using pandas module. Makes the writing to csv very easy:

import arcpy, pandas
fc = r'C:\Test\Cities.shp'
outfile = r'C:\Test\Cities.csv'

fields = ['CityName','SHAPE@X','SHAPE@Y']
df = pandas.DataFrame.from_records(data=arcpy.da.SearchCursor(fc,fields), columns=fields)
df.to_csv(outfile,index=False)


It is also possible to write to excel.