Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in

Posted on

Python Read a csv file

import csv

def read_csv(filename):
    Read a csv file.

    filename : str
        The name of the file to read.

    list of lists
        The contents of the file.

    #create an empty list to store the data
    data = []

    #open the file
    with open(filename, 'r') as f:

        #create a csv reader object
        reader = csv.reader(f)

        #loop over the rows in the csv file
        for row in reader:

            #add each row to the list

    #return the list of lists
    return data

data = read_csv('data.csv')
print(data)  #[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)