Writing To CSV Files

1. Introduction

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.

2. Writing a CSV file

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") # open the file for writing
TheFile.write("1,2,3,4\n") # write a string to the file
TheFile.close() # close the file

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.

while (Index<10): # go through the code below for each value from 0 to 9     
	TheFile.write(format(Index)+"\n")  # Convert the Index to a string and write a line to the file

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.

Row=0 # start the row counter at 0
while (Row<10): # write out 10 rows (0 to 9)
	Column=0 # start the column counter at 0
	while (Column<4): # write out 4 columns (0 to 3)
		TheValue=Row*Column # creating an interesting value
		if (Column>0): # don't write out the comma on the first column, just the subsequent ones
		TheFile.write(format(TheValue)) # convert the value to a string and write it to the file
		Column+=1 # move to the next column
	TheFile.write("\n") # move to the next line
	Row+=1 # move to the next line

3. Adding a Header Line

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

Additional Resources

Python Documentation: Files

© Copyright 2018 HSU - All rights reserved.