How to Read CSV File without any module Python

In this post, we are going to learn how to Read CSV File without any module Python with examples. we will simply use python in-built open() ,rstrip() or split() functions.The open() function is used to read and write files, append a file in python

1. Convert CSV to list without any module


We have used the python inbuilt function open() to open the CSV file in reading mode “r”.The list comprehension to iterate over each row of a csv file and rstrip() method to remove (\n) at end of each line. This is how we read a csv file into the list without any module in Python.

with open('data.csv', 'r') as file:
    
 lines = [line.rstrip() for line in file]
print(lines)

Output

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

2. Read CSV File without any module Python


We have used the python inbuilt function open() to open the CSV file in reading mode “r”.The for loop to iterate over each row of a csv file and split() method to split the rows by comma(‘) delimiter. We have defined an empty list mylist =[] that will store the content of csv file.

with open('data.csv', 'r') as file:
    mylist = []
    for line in file:
            rows = line.split(',')
            mylist.append((rows[0], rows[1:]))
    print(mylist)

Output

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