How to read a huge .csv file with Python?

Sometimes, we want to read a huge .csv file with Python.

In this article, we’ll look at how to read a huge .csv file with Python.

How to read a huge .csv file with Python?

To read a huge .csv file with Python, we can use Pandas’ read_csv method.

For instance, we write

import pandas as pd

chunksize = 10 ** 8

for chunk in pd.read_csv(filename, chunksize=chunksize):
    process(chunk)

to call read_csv with the filename path to the csv file and the chunksize in bytes to read the csv file in chunks.

It returns an iterator that we can use to get the file chunks.

Then we get the csv file chunk from chunk.

Conclusion

To read a huge .csv file with Python, we can use Pandas’ read_csv method.