Categories
Python Answers

How to import csv to list with Python?

Spread the love

Sometimes, we want to import csv to list with Python.

In this article, we’ll look at how to import csv to list with Python.

How to import csv to list with Python?

To import csv to list with Python, we can use the csv module.

For instance, we write

import csv

with open('file.csv', newline='') as f:
    reader = csv.reader(f)
    data = list(reader)

print(data)

to call open to open the file csv.file.

Then we call csv.reader with file f to read the file.

And then we call list on the reader iterator to convert the read csv contents into a list.

Conclusion

To import csv to list with Python, we can use the csv module.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *