jdhao's digital space

Recent content on jdhao's digital space

马上订阅 jdhao's digital space RSS 更新: https://jdhao.github.io/index.xml

How to Read and Write CSV Files in Python

2018年5月13日 21:24
update log

2022-09-30: change order of read and write section; other small fixes.

In this post, I will share the most convenient way to read and write CSV files (with or w/o headers) in Python.

Read CSV files
#

Use the builtin CSV module
#

Python has a built-in CSV module that deals with CSV files. CSV module provides a CSV reader, which we can use to read CSV files. The CSV reader is an iterable object. We can use the following snippet to read CSV files: