Menu Close

How to import csv to list with Python?

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.

Posted in Python, Python Answers