The file format is just that simple: a bunch of data which is divided using a delimiter (sometimes called the separator). Example CSV file data:
1/2/2020,5,5,8,white 1/3/2020,3,5,2,blue 1/4/2020,2,9,1,green
CSV only stores data (values), not formatting. So if you save as a csv file, you lose all formatting you had before. Now for Python programs, formatting is totally irrelevant.
In Python, you can read a csv file. You can then use its data in your program. There are several ways to read a csv file with Python.
- without any module
- read with csv module
- read with pandas module module
If you deal with data a lot, I recommend to use the Pandas module. Pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language.
With the csv module you can read a csv file like this. First import the csv module. Then read the file, define the delimiter and iterate row by row.
import csv with open('example.csv') as csvfile: csvfile = csv.reader(csvfile, delimiter=',') for row in csvfile: print(row)
Every row is a list of values in the csv file.
$ python3 example.py ['1/2/2020', '5', '5', '8', 'white'] ['1/3/2020', '3', '5', '2', 'blue'] ['1/4/2020', '2', '9', '1', 'green']
You're one click away
Level up every day