Writing fields from point shapefile to text file using ArcPy?

The name of the pictureThe name of the pictureThe name of the pictureClash 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))









share|improve this question









New contributor




Jack Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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















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









share|improve this question









New contributor




Jack Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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













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









share|improve this question









New contributor




Jack Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Jack Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Jack Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 hours ago









PolyGeo♦

52.1k1779235




52.1k1779235






New contributor




Jack Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









Jack Jenkins

204




204




New contributor




Jack Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Jack Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Jack Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • 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











  • 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











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)





share|improve this answer




















  • Thank you! I really appreciate your input.
    – Jack Jenkins
    1 hour ago

















up vote
1
down vote













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






share|improve this answer






















  • 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

















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)





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "79"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Jack Jenkins is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    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






























    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)





    share|improve this answer




















    • Thank you! I really appreciate your input.
      – Jack Jenkins
      1 hour ago














    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)





    share|improve this answer




















    • Thank you! I really appreciate your input.
      – Jack Jenkins
      1 hour ago












    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)





    share|improve this answer












    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)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 hours ago









    Aaron♦

    37.1k18105243




    37.1k18105243











    • 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




    Thank you! I really appreciate your input.
    – Jack Jenkins
    1 hour ago












    up vote
    1
    down vote













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






    share|improve this answer






















    • 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














    up vote
    1
    down vote













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






    share|improve this answer






















    • 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












    up vote
    1
    down vote










    up vote
    1
    down vote









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






    share|improve this answer














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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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
















    • 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










    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)





    share|improve this answer
























      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)





      share|improve this answer






















        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)





        share|improve this answer












        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 21 mins ago









        BERA

        12.3k51637




        12.3k51637




















            Jack Jenkins is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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.













             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?