Skip to main content

How to read a CSV file in Python

How to read a CSV file in Python.

Here is a step-by-step tutorial on how to read a CSV file in Python.

  1. First, make sure you have Python installed on your computer. You can download the latest version from the official Python website.

  2. Open your preferred text editor or IDE (Integrated Development Environment) to write your Python code.

  3. Import the CSV module by adding the following line at the beginning of your code:

   import csv
  1. Next, open the CSV file you want to read. You can do this using the open() function, which takes two arguments: the file path and the mode (in this case, "r" for reading). For example, if your CSV file is named "data.csv" and is located in the same directory as your Python script, you can open it like this:
   with open('data.csv', 'r') as file:
# code to read the file will go here

The with open() statement is used to ensure that the file is properly closed after you finish reading it. It is a good practice to use this statement whenever you work with files in Python.

  1. Create a CSV reader object using the csv.reader() function, passing the file object as an argument. This reader object will allow you to iterate over the lines in the CSV file. Add the following code inside the with block:
   csv_reader = csv.reader(file)
  1. Now, you can start reading the contents of the CSV file. You can use a for loop to iterate over the lines in the file. Each line will be returned as a list of values. Add the following code inside the with block, below the previous line:
   for row in csv_reader:
# code to process each row will go here
  1. Within the for loop, you can access the values of each row by indexing the list. For example, if your CSV file has three columns, you can access the values of each column like this:
   for row in csv_reader:
column1 = row[0]
column2 = row[1]
column3 = row[2]

Note that indexing starts at 0, so the first column is at index 0, the second column is at index 1, and so on.

  1. You can perform any operations or data processing you need on the values of each row. For example, you can print the values or store them in variables for further analysis. Here's an example that prints the values of each row:
   for row in csv_reader:
print(row)

If you want to skip the first line (e.g., if it contains headers), you can use the next() function before the for loop. For example:

   next(csv_reader)  # skip the first line
for row in csv_reader:
print(row)
  1. Finally, don't forget to close the file once you've finished reading it. Although the with open() statement takes care of this automatically, it's good practice to explicitly close the file. Add the following line after the for loop:
   file.close()

That's it! You now know how to read a CSV file in Python. Feel free to explore more advanced features of the csv module, such as handling different delimiters or quoting styles, as well as using the DictReader class for reading CSV files with headers.