Comma separate value files or "CSV" files are one of the most common file formats used in research. These are effectively the data from a spreadsheet organized as columns and rows. You can also import and export attribute data from a GIS application using CSVs. Being able to write (and later, read) a CSV file is central to spatial programming.
A CSV file is just a text file with the values separated by commas and a file extension of "CSV". Try the following code and then double-click on the resulting file and it should appear in Excel.
TheFile=open("C:/Temp/test_csv.csv","w") TheFile.write("1,2,3,4\n") TheFile.write("5,6,7,8\n") TheFile.close()
Try adding additional rows and columns to the data. Remember that each row (i.e. each "write()" function) must have the same number of values between the commas.
Now, remember how we used loops to create series of numbers in a previous assignment? Try it again with writing the data to a CSV and then load it into Excel.
TheFile=open("C:/Temp/test2.csv","w") for row in range(0,10): # go through the code below for each value from 0 to 9 TheFile.write(format(row)+"\n") TheFile.close()
We can also write "grids" of values by putting one "for" loop inside another "for" loop. Try the code below with a small number of rows and columns and step through it in the debugger until you feel comfortable with what it is doing. Then, check the contents of the file and try increasing the maximum number of rows and columns and see what happens.
Note that we are now only writing out the "new line" character at the end of each "row" of text.
TheFile=open("C:/Temp/test3.csv","w") for row in range(0,10): # go through the code below for each value from 0 to 9 for column in range(0,4): # go through the code below for each value from 0 to 4 TheValue=row*column # creating an interesting value if (column<>0): TheFile.write(",") # don't write out the comma on the first column, just the subsequent ones TheFile.write(format(TheValue)) TheFile.write("\n") # move to the next line TheFile.close()
You can add a header to a CSV file by just adding a line before your loop. The header will add titles above your columns in Excel and ArcMap. The code below does this and uses a while loop to write out two columns. There is also a PowerPoint presentation that walks through the code.
TheFile=open("C:Temp/Test.csv","w") # open the file for writing TheFile.write("Latitude,Longitude\n") # write the header line Count=0 # setup a counter while (Count<5): # loop until the counter is 5 TheFile.write(format(Count+40)+","+format(Count-120)+"\n") # write two columns of data Count+=1 # add one to the counter TheFile.close() # close the file
Python Documentation: Files
© Copyright 2018 HSU - All rights reserved.