Writing a CSV spreadsheet
CSV files are simple spreadsheets in a highly compatible format. They are text files with tabular data, separated by commas (hence the name Comma-Separated Values), in a simple table format. CSV files can be created using Python's standard library and can be read by all kinds of spreadsheet software.
Getting ready
For this recipe, only the standard library of Python is required. Everything is ready out of the box!
How to do it...
- Import the
csv
module:>>> import csv
- Define the header with how the data will be ordered and the data to store:
>>> HEADER = ('Admissions', 'Name', 'Year') >>> DATA = [ ... (225.7, 'Gone With the Wind', 1939), ... (194.4, 'Star Wars', 1977), ... (161.0, 'ET: The Extra-Terrestrial', 1982) ... ]
- Write the data into a CSV file:
>>> with open('movies.csv...