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