How to efficiently parse fixed width files with Python?

Sometimes, we want to efficiently parse fixed width files with Python.

In this article, we’ll look at how to efficiently parse fixed width files with Python.

How to efficiently parse fixed width files with Python?

To efficiently parse fixed width files with Python, we can use the Pandas’ read_fwf method.

For instance, we write

import pandas as pd

path = 'filename.txt'

col_specification = [(0, 20), (21, 30), (31, 50), (51, 100)]
data = pd.read_fwf(path, colspecs=col_specification)

to define the col_specification list with the column specifications for filename.txt.

Then we call read.fwf with the path and the colspecs argument set to col_specification to parse the fixed with file into a data frame.

Conclusion

To efficiently parse fixed width files with Python, we can use the Pandas’ read_fwf method.