Exporting list of values into csv or txt file using ArcPy?

  • I would like to know how to export in ArcGIS Desktop a list of values calculated in Python script into one of the following data formats: csv, txt, xls, dbase or other. I would also like to know how to create such file in case that it doesnt exist.

    The list of values looks like res=(1,2,3,...,x). Each value must be written into a new row.

  • Paul

    Paul Correct answer

    7 years ago

    You mention that you computed a list of values in a Python script, so the easiest way to dump that to a csv would be to use the csv module!

    import csv
    
    res = [x, y, z, ....]
    csvfile = "<path to output csv or txt>"
    
    #Assuming res is a flat list
    with open(csvfile, "w") as output:
        writer = csv.writer(output, lineterminator='\n')
        for val in res:
            writer.writerow([val])    
    
    #Assuming res is a list of lists
    with open(csvfile, "w") as output:
        writer = csv.writer(output, lineterminator='\n')
        writer.writerows(res)
    

    This is exactly what I was looking for. However it looks that the csv file must already exist. What if it doesnt. How can I create it programmatically?

    @user21816, by using the "w" parameter (short for 'write') for the open() function, you can create a new file at that path.

    this method worked for me! my question is this, what if you want the output csv table to be oriented the opposite way from this method?

    @AndyWitt, do you mean transposing the rows and columns?

    @Paul, that exactly my question

  • QGIS 1.8 can't edit a CSV file. The workaround is to import the csv file into a db.sqlite table using QGIS's Qspatialite or Spatialite_GUI etc., and then edit the table and export that data back into a table.csv file, if necessary. In QGIS 1.8, DONT export or import into sqlite or spatialite directly from under LAYERS, via right-clicking. It is very slow and may crash. Use the Qspatialite plugin instead to load sqlite databases, and right click from Qspatialite to load into LAYERS for QGIS editing.

    Alternately, you can right click on the table.csv file under your QGIS 1.8 LAYERS, export to shapefile, then load "vector" file, changing the file extension to ".*" to see ALL files available, including dbf without associated shapes. It loads the dbf table which can be edited, but if your column name or data widths exceed the shapefile/dbf limit then the data will be truncated. After importing back into a csv file, the table names can easily be restored with a text or spreadsheet editor, for instance Notepad, Gedit or Excel. That additional information is for the posterity of future folks looking over this question for an answer that suits their needs.

  • You could use pandas:

    In [1]: import pandas as pd
            res = [[1, 2, 3, 4, 5], ['a', 'b', 'c', 'd', 'e']]
            my_df = pd.DataFrame(res)
            my_df.to_csv('out.csv', index=False, header=False)
            print my_df
    
               0  1  2  3  4
            0  1  2  3  4  5
            1  a  b  c  d  e
    

    pandas is a great module for easy data handling and analysis.

    To clarify the output for others, the numbers on top and on the left are indexes that jupyter notebook automatically inserts and not something you'll see it in your `csv` file because of the named argument given with `header=False` above.

  • You can use this function:

    import arcpy
    import csv #if you have unicode characters in your table, use: import unicodecsv as csv
    def export_table_as_txt(infile,outfile):
    '''
    Exports a feature classes table to a txt file.
    '''
    #--first lets make a list of all of the fields in the table
    fields = arcpy.ListFields(infile)
    field_names = [field.name for field in fields]
    
    with open(outfile,'wb') as f:
        dw = csv.DictWriter(f,field_names,delimiter=';')
        #--write all field names to the output file
        dw.writeheader()
        #--now we make the search cursor that will iterate through the rows of the table
        with arcpy.da.SearchCursor(infile,field_names) as cursor:
            for row in cursor:
                dw.writerow(dict(zip(field_names,row)))
    

    In the line after the first with statement, you can choose the delimiter. Use \t instead of ; if you want tab delimited output.

    Then, call the function with something like this:

    layer = "C:\my_folder\my_layer.shp" #can also be a path to a featureclass in a Geo-Database
    export_outfile = "C:\my_folder\my_output.txt"
    export_table_as_txt(layer,export_outfile)
    

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM