---
description: In this Python tutorial, you will learn How to Read and Write CSV Files using Module &amp; Pandas, Python CSV Module, and Download Sample CSV File for Python.
title: How to Read CSV File in Python (Module, Pandas Examples)
image: https://www.guru99.com/images/1/122118_0714_ReadingandW1.png
---

[Skip to content](#main) 

## What is a CSV file?

A **CSV file** is a simple type of plain text file which uses a specific structure to arrange tabular data. The standard format of a CSV file is defined by rows and columns data where a newline terminates each row to begin the next row, and each column is separated by a comma within the row.

CSV is a common format for data interchange as it is compact, simple, and general. Many online services allow their users to export tabular data from the website into a CSV file. Files of CSV will open into Excel, and nearly all databases have a tool to allow import from CSV files.

## CSV Sample File

Data in the form of tables is also called CSV (comma separated values) – literally “comma-separated values.” This is a text format intended for the presentation of tabular data. Each line of the file is one line of the table. The values of individual columns are separated by a separator symbol – a comma (,), a semicolon (;) or another symbol. CSV can be easily read and processed by Python.

Consider the following Table  

### Table Data

| Programming language | Designed by       | Appeared | Extension |
| -------------------- | ----------------- | -------- | --------- |
| Python               | Guido van Rossum  | 1991     | .py       |
| Java                 | James Gosling     | 1995     | .java     |
| C++                  | Bjarne Stroustrup | 1983     | .cpp      |

You can represent this table in csv as below.

### CSV Data

Programming language, Designed by, Appeared, Extension

Python, Guido van Rossum, 1991, .py

Java, James Gosling, 1995, .java

C++, Bjarne Stroustrup,1983,.cpp

As you can see each row is a new line, and each column is separated with a comma. This is an example of how a CSV file looks like.

[Download CSV Data](https://drive.google.com/uc?export=download&id=1e4zXe62E41RqLexYusECd5RSvN5aVZGI)

## Python CSV Module

[Python](https://www.guru99.com/python-tutorials.html) provides a CSV module to handle CSV files. To read/write data, you need to loop through rows of the CSV. You need to use the split method to get data from specified columns.

## CSV Module Functions

In CSV module documentation you can find following functions:

* csv.field\_size\_limit – return maximum field size
* csv.get\_dialect – get the dialect which is associated with the name
* csv.list\_dialects – show all registered dialects
* csv.reader – read data from a csv file
* csv.register\_dialect – associate dialect with name
* csv.writer – write data to a csv file
* csv.unregister\_dialect – delete the dialect associated with the name the dialect registry
* **csv.QUOTE\_ALL** – Quote everything, regardless of type.
* **csv.QUOTE\_MINIMAL** – Quote fields with special characters
* **csv.QUOTE\_NONNUMERIC** – Quote all fields that aren’t numbers value
* **csv.QUOTE\_NONE** – Don’t quote anything in output

In this tutorial, we are going to focus only on the reader and writer functions which allow you to edit, modify, and manipulate the data in a CSV file.

## How to Read a CSV File in Python

Below are steps to read CSV file in Python.

**Step 1)** To read data from CSV files, you must use the reader function to generate a reader object.

The reader function is developed to take each row of the file and make a list of all columns. Then, you have to choose the column you want the variable data for.

It sounds a lot more intricate than it is. Let’s take a look at this Python code to read CSV file, and we will find out that working with csv file isn’t so hard.

#import necessary modules
import csv
with open('X:\data.csv','rt')as f:
  data = csv.reader(f)
  for row in data:
        print(row)

**Step 2)** When you execute the program above, the output will be:

['Programming language; Designed by; Appeared; Extension']
['Python; Guido van Rossum; 1991; .py']
['Java; James Gosling; 1995; .java']
['C++; Bjarne Stroustrup;1983;.cpp']

### RELATED ARTICLES

* [ Operators in Python – Logical, Arithmetic, Comparison ](https://www.guru99.com/python-operators-complete-tutorial.html "Operators in Python – Logical, Arithmetic, Comparison")
* [ Python Tutorial for Beginners (Free PDF) ](https://www.guru99.com/python-tutorials.html "Python Tutorial for Beginners (Free PDF)")
* [ Swap two numbers without using a third variable: C, Python Program ](https://www.guru99.com/swap-two-numbers-without-using-third-variable.html "Swap two numbers without using a third variable: C, Python Program")
* [ Python Escape Character Sequences (Examples) ](https://www.guru99.com/python-escape-characters.html "Python Escape Character Sequences (Examples)")

## How to read a CSV file into a Dictionary in Python

You can also you use DictReader to read CSV files. The results are interpreted as a dictionary where the header row is the key, and other rows are values.

Consider the following code

#import necessary modules
import csv

reader = csv.DictReader(open("file2.csv"))
for raw in reader:
    print(raw)

The result of this code is:

OrderedDict([('Programming language', 'Python'), ('Designed by', 'Guido van Rossum'), (' Appeared', ' 1991'), (' Extension', ' .py')])
OrderedDict([('Programming language', 'Java'), ('Designed by', 'James Gosling'), (' Appeared', ' 1995'), (' Extension', ' .java')])
OrderedDict([('Programming language', 'C++'), ('Designed by', ' Bjarne Stroustrup'), (' Appeared', ' 1985'), (' Extension', ' .cpp')])

[](https://www.guru99.com/images/1/122118%5F0714%5FReadingandW1.png)

And this way to read data from CSV file is much easier than earlier method. However, this is not isn’t the best way to read data.

## How to write CSV File in Python

Here is how to write a CSV file in Python:

When you have a set of data that you would like to store in a CSV file you have to use writer() function. To iterate the data over the rows(lines), you have to use the writerow() function.

Consider the following example. We write data into a file “writeData.csv” where the delimiter is an apostrophe.

#import necessary modules
import csv

with open('X:\writeData.csv', mode='w') as file:
    writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    #way to write to csv file
    writer.writerow(['Programming language', 'Designed by', 'Appeared', 'Extension'])
    writer.writerow(['Python', 'Guido van Rossum', '1991', '.py'])
    writer.writerow(['Java', 'James Gosling', '1995', '.java'])
    writer.writerow(['C++', 'Bjarne Stroustrup', '1985', '.cpp'])

Result in csv file is:

Programming language, Designed by, Appeared, Extension 
Python, Guido van Rossum, 1991, .py
Java, James Gosling, 1995, .java
C++, Bjarne Stroustrup,1983,.cpp

[](https://www.guru99.com/images/1/122118%5F0714%5FReadingandW2.png)

## Read CSV File using Pandas

Pandas is an opensource library that allows to you import CSV in Python and perform data manipulation. Pandas provide an easy way to create, manipulate and delete the data.

You must install pandas library with command <code>pip install pandas</code>. In Windows, you will execute this command in Command Prompt while in Linux in the Terminal.

Reading the CSV into a pandas DataFrame is very quick and easy:

#import necessary modules
import pandas
result = pandas.read_csv('X:\data.csv')
print(result)

Result of the [read CSV Pandas](https://www.guru99.com/pandas-read-csv.html) example:

Programming language, Designed by, Appeared, Extension 
0    Python, Guido van Rossum, 1991, .py
1    Java, James Gosling, 1995, .java
2    C++, Bjarne Stroustrup,1983,.cpp

Very useful library. In just three lines of code you the same result as earlier. Pandas know that the first line of the CSV contained column names, and it will use them automatically.  

## Write CSV File using Pandas

Writing to CSV file with Pandas is as easy as reading. Here you can convince in it. First you must create DataFrame based on the following Python write to CSV code.

from pandas import DataFrame
C = {'Programming language': ['Python','Java', 'C++'],
        'Designed by': ['Guido van Rossum', 'James Gosling', 'Bjarne Stroustrup'],
        'Appeared': ['1991', '1995', '1985'],
        'Extension': ['.py', '.java', '.cpp'],
    }
df = DataFrame(C, columns= ['Programming language', 'Designed by', 'Appeared', 'Extension'])
export_csv = df.to_csv (r'X:\pandaresult.csv', index = None, header=True) # here you have to write path, where result file will be stored
print (df)

Here is the output

Programming language, Designed by, Appeared, Extension
0    Python, Guido van Rossum, 1991, .py
1    Java, James Gosling, 1995, .java
2    C++, Bjarne Stroustrup,1983,.cpp

And CSV file is created at the specified location.

[](https://www.guru99.com/images/1/122118%5F0714%5FReadingandW3.png)

## Conclusion

So, now you know how use method ‘csv’ and also read and write data in CSV format. CSV files are widely used in software applications because they are easy to read and manage, and their small size makes them relatively fast for processing and transmission.

The csv module provides various functions and classes which allow you to read and write easily. You can look at the official Python documentation and find some more interesting tips and modules. CSV is the best way for saving, viewing, and sending data. Actually, it isn’t so hard to learn as it seems at the beginning. But with a little practice, you’ll master it.

Pandas is a great alternative to read CSV files.

Also, there are other ways to parse text files with libraries like ANTLR, PLY, and PlyPlus. They can all handle heavy-duty parsing, and if simple String manipulation doesn’t work, there are regular expressions which you can use.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

 You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search 

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/python-csv.png","url":"https://www.guru99.com/images/python-csv.png","width":"700","height":"197","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/python-csv.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/python","name":"Python"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/python-csv.html","name":"How to Read CSV File in Python (Module, Pandas Examples)"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/python-csv.html#webpage","url":"https://www.guru99.com/python-csv.html","name":"How to Read CSV File in Python (Module, Pandas Examples)","dateModified":"2024-08-12T16:10:01+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/python-csv.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/python-csv.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/anna","name":"Anna Blake","description":"I'm Anna Blake, specializing in Python tutorials, offering clear and concise lessons to help you master Python programming efficiently.","url":"https://www.guru99.com/author/anna","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/anna-blake-author.png","url":"https://www.guru99.com/images/anna-blake-author.png","caption":"Anna Blake","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"image":{"@id":"https://www.guru99.com/images/python-csv.png"},"headline":"How to Read CSV File in Python (Module, Pandas Examples)","description":"In this Python tutorial, you will learn How to Read and Write CSV Files using Module &amp; Pandas, Python CSV Module, and Download Sample CSV File for Python.","keywords":"python","@type":"Article","author":{"@id":"https://www.guru99.com/author/anna","name":"Anna Blake"},"dateModified":"2024-08-12T16:10:01+05:30","copyrightYear":"2024","name":"How to Read CSV File in Python (Module, Pandas Examples)","articleSection":"Python","subjectOf":[{"@type":"HowTo","name":"How to Read a CSV File?","description":"Here is how to read CSV file in Python:","step":[{"@type":"HowToStep","name":"Step 1) To read data from CSV files, you must use the reader function to generate a reader object.","text":"The reader function is developed to take each row of the file and make a list of all columns. Then, you have to choose the column you want the variable data for.","url":"https://www.guru99.com/python-csv.html#step1"},{"@type":"HowToStep","name":"Step 2) When you execute the program above,","text":"the output will be:'Programming language; Designed by; Appeared; Extension'","url":"https://www.guru99.com/python-csv.html#step2"}]}],"@id":"https://www.guru99.com/python-csv.html#schema-23305","isPartOf":{"@id":"https://www.guru99.com/python-csv.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/python-csv.html#webpage"}}]}
```
