How to import a csv-file into a data array?

Assuming the CSV file is delimited with commas, the simplest way using the csv module in Python 3 would probably be:

import csv

with open('testfile.csv', newline='') as csvfile:
    data = list(csv.reader(csvfile))

print(data)

You can specify other delimiters, such as tab characters, by specifying them when creating the csv.reader:

    data = list(csv.reader(csvfile, delimiter='\t'))

For Python 2, use open('testfile.csv', 'rb') to open the file.[

Leave a Comment