9.12 Intro to Data Science: Working with CSV Files

Throughout this book, you’ll work with many datasets as you learn data-science concepts. CSV (comma-separated values) is a particularly popular file format. In this section, we’ll demonstrate CSV file processing with a Python Standard Library module and pandas.

9.12.1 Python Standard Library Module csv

The csv module11 provides functions for working with CSV files. Many other Python libraries also have built-in CSV support.

Writing to a CSV File

Let’s create an accounts.csv file using CSV format. The csv module’s documentation recommends opening CSV files with the additional keyword argument newline='' to ensure that newlines are processed properly: ...

Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.