CSV files are the most common way to share data between tools, from a billing export to a survey. Python can read and write them with a few lines and the built-in csv module.
Reading a file
Open the file, pass it to csv.reader, and loop over the rows. The first row is usually the header, so read it separately and use it as the column names.
Writing a file
Open a file in write mode, create a csv.writer, and write each row as a list. Use a with block so the file closes even if an error happens.
Two checks
- Check the number of rows you read against the number in the source file.
- Convert text to numbers before adding them, because CSV values are always read as text.
For larger files, pandas is faster to work with. See pandas for beginners, or Python for automation. Book a free demo class, and ask a counsellor for batch details.
