What Are CSV Files?

This lesson introduces Comma Separated Values(CSV) files, a plain text file format commonly used for input/output of programs. In this video, you learned that CSV files:

  • Are a common input/output file type for programs
  • Are plain text files that contain no non-printable characters
  • Easy to work with programmatically
  • Contain different sections which are separated by a comma or other delimeter
Download

Sample CSV Files (.zip)

2.3 KB

00:00 Hey there! And welcome to the Real Python guide to Reading and Writing CSV Files in Python. In this set of videos, you’ll use two different libraries to interact with CSVs. First, you’ll see how Python’s built-in csv library can be used to read and write CSV files. Next, you’ll learn to use pandas to work with CSVs, which is a powerful library geared towards data analysis. So, let’s go!

00:26 So, what are CSVs—also known as Comma-Separated Values files?

00:32 CSVs are a common input and output file type for programs.

00:36 They store data in a plain text file using only printable characters, and unlike most text files, they’re easy to work with programmatically, due to their comma-separated structure. Let’s take a closer look at that structure.

00:50 Here’s the basic outline for a CSV file. Note that there are different sections separated by commas—hence the name. When you parse this file, each of these sections will be treated individually, and you’ll be able to access each piece as you need to.

01:05 It’s also normal to keep header information in the top row, which then describes what the data in each column below contains. Note that not all CSVs are separated by commas.

01:15 Here’s a CSV that uses the pipe operator character as its delimiter. The delimiter is just the character—or set of characters—that’s used to split up each row. We’ll talk more about using different delimiters later on, but for now, let’s get to the text editor and start using Python to work with CSVs. Thanks for watching!

sweir12525 on Aug. 17, 2020

I am having a very difficult time trying to import a cvs file into whatever interpreter you are using in this tutorial

I put a file on my desktop and tried to import it and read it, as follows:

import csv

with open(‘C:\Desktop\Phyton_Projects\instructor.cvs’) as cvs_file: csv_reader = csv.reader(csv_file, delimiter=’,’) line_count = 0 for row in csv_reader: if line_count ==0: print(f’column names are {“,”.join (row)}’) line_count +=1 else: print(f’\t{row[1]} taught course {row[0]}, at host location {row[4]}’)

I am getting an error saying in Thorny saying I need a r prefix but I don’t know where to put it. Can you help?

Geir Arne Hjelle RP Team on Aug. 17, 2020

@sweir12525

Backslashes (\) have special meanings in Python (and many other programming languages) as escape sequences that allow you specify special characters. A couple of the most common examples are \n which represents a newline (enter) and \t which represent a tab character.

Unfortunately, backslashes are also used by file paths in Windows. Python is not able to interpret the path to your CSV file because of the backslashes being interpreted as special characters.

The easiest way to work around this is to tell Python that the path is something called a raw string (or r-string) by prefixing it with an r like Thonny suggests. You would do that by putting an r immediately in front of the opening quote when specifying the path:

with open(r'C:\Desktop\Phyton_Projects\instructor.cvs’) as cvs_file:
    ...

Become a Member to join the conversation.