One of the primary tasks I personally use Python for is data collection and manipulation. In this post I’ll walk you through some basic CSV file work.
Reading from a CSV File
Take the CSV file below, it’s a collection of collected ADC points with the acquisition number preceding it. We’re going to take this CSV file, read it in Python, and display it in the command prompt.
1 2 3 4 5 6 7 8 9 10 |
1, 3160, 2, 3156, 3, 3091, 4, 3159, 5, 3132, 6, 3104, 7, 3124, 8, 3085, 9, 3100, 10, 3095, |
The Python script below is an example of how this can be done. Walking through the file, we first import the csv definitions. This provides us access to all of the functions in pycsv. Next, we open the file and assign it to the csvfile object. Then we read the file with the specified delimiter. Finally, we cycle through the file and print the parsed contents one at a time.
1 2 3 4 5 6 7 8 9 |
import csv #imports CSV definitions with open('csvDataSample.csv') as csvfile: # Target our sample CSV file and create the csv file object readCSV = csv.reader(csvfile, delimiter=',') # Read the file and specify the delimiter # Cycle through the file and print the contents for row in readCSV: #print(row) print("Sample " + row[0] + " value is " + row[1]) |
The results of running the script in the Enhanced Console can be seen in the capture below.
Writing to a CSV File
Writing to a CSV file is similar. The primary deltas are they you create a write object and write the needed values to a row. One caveat to the casting done on line 11, is that the data read from the CSV file are all text, so if you want to do math to it then you need to cast it as an integer, do the math, then cast that result back to a string to write it to the file.
1 2 3 4 5 6 7 8 9 10 11 |
import csv c = csv.writer(open("output.csv", "wb")) with open('csvDataSample.csv') as csvfile: readCSV = csv.reader(csvfile, delimiter=',') for row in readCSV: #print(row) print("Sample " + row[0] + " value is " + row[1]) c.writerow([row[0],row[1],str(int(row[1])+10)]) |
This results in the output file below.
1 2 3 4 5 6 7 8 9 10 |
1, 3160,3170 2, 3156,3166 3, 3091,3101 4, 3159,3169 5, 3132,3142 6, 3104,3114 7, 3124,3134 8, 3085,3095 9, 3100,3110 10, 3095,3105 |