Writing fields from point shapefile to text file using ArcPy?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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))
arcpy cursor
New contributor
add a comment |Â
up vote
1
down vote
favorite
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))
arcpy cursor
New contributor
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
â Jack Jenkins
3 hours ago
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.
â Jack Jenkins
2 hours ago
Please update your question post (using the 'edit' button underneath it) to include a description of the problem. Eg, 'the script produces no output, no errors, and writes nothing to the CSV file'.
â Son of a Beach
2 hours ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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))
arcpy cursor
New contributor
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))
arcpy cursor
arcpy cursor
New contributor
New contributor
edited 2 hours ago
PolyGeoâ¦
52.1k1779235
52.1k1779235
New contributor
asked 3 hours ago
Jack Jenkins
204
204
New contributor
New contributor
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
â Jack Jenkins
3 hours ago
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.
â Jack Jenkins
2 hours ago
Please update your question post (using the 'edit' button underneath it) to include a description of the problem. Eg, 'the script produces no output, no errors, and writes nothing to the CSV file'.
â Son of a Beach
2 hours ago
add a comment |Â
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
â Jack Jenkins
3 hours ago
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.
â Jack Jenkins
2 hours ago
Please update your question post (using the 'edit' button underneath it) to include a description of the problem. Eg, 'the script produces no output, no errors, and writes nothing to the CSV file'.
â Son of a Beach
2 hours ago
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
â Jack Jenkins
3 hours ago
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
â Jack Jenkins
3 hours ago
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.
â Jack Jenkins
2 hours ago
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.
â Jack Jenkins
2 hours ago
Please update your question post (using the 'edit' button underneath it) to include a description of the problem. Eg, 'the script produces no output, no errors, and writes nothing to the CSV file'.
â Son of a Beach
2 hours ago
Please update your question post (using the 'edit' button underneath it) to include a description of the problem. Eg, 'the script produces no output, no errors, and writes nothing to the CSV file'.
â Son of a Beach
2 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
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:DataCities.shp"'
txt = r'C:pathtoyourtxt_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)
Thank you! I really appreciate your input.
â Jack Jenkins
1 hour ago
add a comment |Â
up vote
1
down vote
I can see four problems with your script:
It looks like your script is attempting to write to the shapefile (
citiesFile
) instead of to the CSV file (COCitiesFile
).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 yourarcpy.env.
line as it is unused).You also need to include the city name field in your search cursor and your file writing.
Your
row
variable is undefined (are you sure your script produced no errors?). You should iterate through the search cursor using afor
loop and can use arow
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).
I really appreciate your time and effort in helping me figure this out. I'll look into this further, with a fresh mind tomorrow and let you know my outcome.
â Jack Jenkins
1 hour ago
add a comment |Â
up vote
1
down vote
Another way using pandas module. Makes the writing to csv very easy:
import arcpy, pandas
fc = r'C:TestCities.shp'
outfile = r'C:TestCities.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)
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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:DataCities.shp"'
txt = r'C:pathtoyourtxt_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)
Thank you! I really appreciate your input.
â Jack Jenkins
1 hour ago
add a comment |Â
up vote
1
down vote
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:DataCities.shp"'
txt = r'C:pathtoyourtxt_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)
Thank you! I really appreciate your input.
â Jack Jenkins
1 hour ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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:DataCities.shp"'
txt = r'C:pathtoyourtxt_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)
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:DataCities.shp"'
txt = r'C:pathtoyourtxt_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)
answered 2 hours ago
Aaronâ¦
37.1k18105243
37.1k18105243
Thank you! I really appreciate your input.
â Jack Jenkins
1 hour ago
add a comment |Â
Thank you! I really appreciate your input.
â Jack Jenkins
1 hour ago
Thank you! I really appreciate your input.
â Jack Jenkins
1 hour ago
Thank you! I really appreciate your input.
â Jack Jenkins
1 hour ago
add a comment |Â
up vote
1
down vote
I can see four problems with your script:
It looks like your script is attempting to write to the shapefile (
citiesFile
) instead of to the CSV file (COCitiesFile
).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 yourarcpy.env.
line as it is unused).You also need to include the city name field in your search cursor and your file writing.
Your
row
variable is undefined (are you sure your script produced no errors?). You should iterate through the search cursor using afor
loop and can use arow
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).
I really appreciate your time and effort in helping me figure this out. I'll look into this further, with a fresh mind tomorrow and let you know my outcome.
â Jack Jenkins
1 hour ago
add a comment |Â
up vote
1
down vote
I can see four problems with your script:
It looks like your script is attempting to write to the shapefile (
citiesFile
) instead of to the CSV file (COCitiesFile
).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 yourarcpy.env.
line as it is unused).You also need to include the city name field in your search cursor and your file writing.
Your
row
variable is undefined (are you sure your script produced no errors?). You should iterate through the search cursor using afor
loop and can use arow
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).
I really appreciate your time and effort in helping me figure this out. I'll look into this further, with a fresh mind tomorrow and let you know my outcome.
â Jack Jenkins
1 hour ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I can see four problems with your script:
It looks like your script is attempting to write to the shapefile (
citiesFile
) instead of to the CSV file (COCitiesFile
).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 yourarcpy.env.
line as it is unused).You also need to include the city name field in your search cursor and your file writing.
Your
row
variable is undefined (are you sure your script produced no errors?). You should iterate through the search cursor using afor
loop and can use arow
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).
I can see four problems with your script:
It looks like your script is attempting to write to the shapefile (
citiesFile
) instead of to the CSV file (COCitiesFile
).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 yourarcpy.env.
line as it is unused).You also need to include the city name field in your search cursor and your file writing.
Your
row
variable is undefined (are you sure your script produced no errors?). You should iterate through the search cursor using afor
loop and can use arow
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).
edited 2 hours ago
answered 2 hours ago
Son of a Beach
1,205618
1,205618
I really appreciate your time and effort in helping me figure this out. I'll look into this further, with a fresh mind tomorrow and let you know my outcome.
â Jack Jenkins
1 hour ago
add a comment |Â
I really appreciate your time and effort in helping me figure this out. I'll look into this further, with a fresh mind tomorrow and let you know my outcome.
â Jack Jenkins
1 hour ago
I really appreciate your time and effort in helping me figure this out. I'll look into this further, with a fresh mind tomorrow and let you know my outcome.
â Jack Jenkins
1 hour ago
I really appreciate your time and effort in helping me figure this out. I'll look into this further, with a fresh mind tomorrow and let you know my outcome.
â Jack Jenkins
1 hour ago
add a comment |Â
up vote
1
down vote
Another way using pandas module. Makes the writing to csv very easy:
import arcpy, pandas
fc = r'C:TestCities.shp'
outfile = r'C:TestCities.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)
add a comment |Â
up vote
1
down vote
Another way using pandas module. Makes the writing to csv very easy:
import arcpy, pandas
fc = r'C:TestCities.shp'
outfile = r'C:TestCities.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)
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Another way using pandas module. Makes the writing to csv very easy:
import arcpy, pandas
fc = r'C:TestCities.shp'
outfile = r'C:TestCities.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)
Another way using pandas module. Makes the writing to csv very easy:
import arcpy, pandas
fc = r'C:TestCities.shp'
outfile = r'C:TestCities.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)
answered 21 mins ago
BERA
12.3k51637
12.3k51637
add a comment |Â
add a comment |Â
Jack Jenkins is a new contributor. Be nice, and check out our Code of Conduct.
Jack Jenkins is a new contributor. Be nice, and check out our Code of Conduct.
Jack Jenkins is a new contributor. Be nice, and check out our Code of Conduct.
Jack Jenkins is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f299302%2fwriting-fields-from-point-shapefile-to-text-file-using-arcpy%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
â Jack Jenkins
3 hours ago
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.
â Jack Jenkins
2 hours ago
Please update your question post (using the 'edit' button underneath it) to include a description of the problem. Eg, 'the script produces no output, no errors, and writes nothing to the CSV file'.
â Son of a Beach
2 hours ago