Convert CSV to tuple python

In this post, we are going to learn how to Convert CSV to a tuple in python with the help of the Python CSV module and Python Pandas library. The Python CSV module is used to read and write CSV files. To read a file csv.reader() method is used.To run all programs make sure Pandas and CSV libraries are installed on the local machines.

1. Convert CSV to tuple Python


The CSV module is used to read a csv file. First, we have opened the CSV file in reading mode “r” and passed the file object to csv. reader() method returns an iterator. We have used this iterator to iterate over each row of the CSV file using list comprehension and printing the content of the CSV file.

  • The python CSV module using “import csv”
  • list comprehension to convert contents into a list of tuples.
  • The list() to convert tuples into a list of tuples
import csv

with open('data.csv', newline='') as file:
    content = csv.reader(file)
    CSV_toTuple= [tuple(row) for row in content]

print(CSV_toTuple)

Output

[('Name', 'Marks', 'Address', 'Subject'), ('Tony', '98', 'Chicago', 'Math'), ('Jack', '100', 'US', 'English'), ('John', '100', 'India', 'Music'), ('Rim', '100', 'Canda', 'Chemistry')]

2. list comprehension to convert CSV to tuple


We have used the same approach as in the above Python program with a short one-line code. First, open the CSV file in reading mode passed the file object to csv. reader() function returns an iterator and list comprehension to iterate over the contents and display the list of tuples.

import csv

CSV_toTuple = [tuple(row) for row in csv.reader(open('data.csv', 'r'))]
print(CSV_toTuple)

Output

[('Name', 'Marks', 'Address', 'Subject'), ('Tony', '98', 'Chicago', 'Math'), ('Jack', '100', 'US', 'English'), ('John', '100', 'India', 'Music'), ('Rim', '100', 'Canda', 'Chemistry')]

3. CSV to tuples using map and list comprehension


The CSV module is used to read a csv file, First, we have opened the CSV file in reading mode “r” and passed the file object to csv. reader() method returns an iterator and list comprehension to iterate over each row of the CSV file.

The map() function is used to apply the tuple() function to each row of the CSv file and converts it into tuples and list() to convert tuple into a list of tuples.

  • Import the python CSv module using “import csv”
  • map to apply tuple function on each row
  • The list() to convert tuples into a list of tuples
import csv

with open('data.csv', 'r') as File:
   
    csvreader = csv.reader(File)
    
    CSV_to_tuple = list(map(tuple, csvreader))
    
    print(CSV_to_tuple)

Output

[('Name', 'Marks', 'Address', 'Subject'), ('Tony', '98', 'Chicago', 'Math'), ('Jack', '100', 'US', 'English'), ('John', '100', 'India', 'Music'), ('Rim', '100', 'Canda', 'Chemistry')]

4. Convert CSV to tuple using Pandas


We have loaded the contents of the file in Pandas dataframe by using pd.read_csv() method that returns a numpy 2D array. We have iterated over the 2D array using list comprehension that returns a list of tuples. Let us understand with the below example

import pandas as pd

dfobj = pd.read_csv('data.csv', delimiter=',')


CSv_list_of_tuple = [tuple(row) for row in dfobj.values]

print(CSv_list_of_tuple)

Output

[('Tony', 98, 'Chicago', 'Math'), ('Jack', 100, 'US', 'English'), ('John', 100, 'India', 'Music'), ('Rim', 100, 'Canda', 'Chemistry')]

Summary

In this post, we have learned how to Convert CSV to a tuple in python with python program examples by using CSV and Pandas libaray.