Prerequisites: Working with csv files in Python
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.
CSV raw data is not utilizable in order to use that in our Python program it can be more beneficial if we could read and separate commas and store them in a data structure. We can convert data into lists or dictionaries or a combination of both either by using functions csv.reader and csv.dictreader or manually directly
and in this article, we will see it with the help of code.
Example 1: Loading CSV to list
CSV File:

# importing module import csv # csv fileused id Geeks.csv filename="Geeks.csv" # opening the file using "with" # statement with open(filename,'r') as data: for line in csv.reader(data): print(line) # then data is read line by line # using csv.reader the printed # result will be in a list format # which is easy to interpret |
Output:

Example 2: Loading CSV to dictionary
import csv filename ="Geeks.csv" # opening the file using "with" # statement with open(filename, 'r') as data: for line in csv.DictReader(data): print(line) |
Output:

Recommended Posts:
- Python program to read CSV without CSV module
- Create a GUI to convert CSV file into excel file using Python
- How to save a Python Dictionary to a CSV File?
- Writing data from a Python List to CSV row-wise
- Python - Convert Dictionary Value list to Dictionary List
- Convert HTML table into CSV file in python
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Using csv module to read the data in Pandas
- Convert a NumPy array into a csv file
- Load JSON String into Pandas DataFrame
- Python - Save List to CSV
- Python program to update a dictionary with the values from a dictionary list
- How to Convert an image to NumPy array and saveit to CSV file using Python?
- How to load and save 3D Numpy array to file using savetxt() and loadtxt() functions?
- Python | Read csv using pandas.read_csv()
- Convert CSV to Excel using Pandas in Python
- Convert CSV to JSON using Python
- Convert Text File to CSV using Python Pandas
- Python | Convert a list of Tuples into Dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

