Using CSV files to store data
Now you know methods to open, manipulate, and close files using Python. In the previous examples, we used the Python interpreter and string data to get familiar with these methods. But when it comes to saving a large number of numerical values from sensor data, the comma separated values (CSV) file format is one of the most widely used file formats other than text. As the name states, values are separated and stored using commas or other delimiters such as a space or tab. Python has a built-in module to deal with CSV files.
To begin with, use the following code snippet to create a Python file and run your first CSV program:
import csv data = [[1, 2, 3], ['a', 'b', 'c'], ['Python', 'Arduino', 'Programming']] with open('example.csv', 'w') as f: w = csv.writer(f) for row in data: w.writerow(row)
You can also open the csvWriter.py
file from this chapter's code folder, which contains the same code. After executing the code, you will be able to find a file named...