Python Convert Fixed Width File to CSV

What is a Fixed-Width File?

πŸ’‘ Definition: A fixed-width text file contains data that is structured in rows and columns. Each row contains one data entry consisting of multiple values (one value per column). Each column has a fixed width, i.e., the same number of characters, restricting the maximum data size per column.

Example of a Fixed-Width File

Here’s an example of the fixed-width file format as a screenshot of 'my_file.fwf' with the abbreviated “Fixed Width File” suffix .fwf:

Here’s the same file format for copy&pasting:

Name      Age     Job          Income
Alice     24      Programmer   99000
Bob       28      Scientist    44000
Carl      44      Manager      129000
Dave      18      Secretary    29000

How to Convert a Fixed-Width File (.fwf) to a Comma-Separated Values (.csv) File?

The pure Python (no-library) way of converting a fixed-width file to a comma-separated values (CSV) file uses four main steps:

  1. Open the input (.fwf) and output (.csv) files
  2. Read the input data as a list of strings (one fixed-width string per line)
  3. Convert the list of fixed-width strings to a list of lists (one inner list per row)
  4. Write each row using a comma as a separator

Here’s the code solution to this problem:

# Open the input (.fwf) and output (.csv) files
with open('my_file.fwf') as infile:
    with open('my_file.csv', 'w') as outfile:

        # Read the input data as list of strings
        data = infile.readlines()

        # Convert list of fixed-width strings to list of lists
        csv_data = [line.split() for line in data]

        # Write each row using comma as separator
        for row in csv_data:
            outfile.write(','.join(row) + '\n')
            

If the input file looks like this:

The resulting CSV file looks like this:

How to Convert a Fixed-Width File to a CSV in Pandas?

You can convert a fixed-width file to a CSV using Python pandas by reading the fixed-width file as a DataFrame df using pd.read('my_file.fwf') and writing the DataFrame to a CSV using df.to_csv('my_file.csv', index=False).

Here’s the code solution:

import pandas as pd

df = pd.read_fwf('my_file.fwf')
df.to_csv('my_file.csv', index=False)

Again, if the input file looks like this:

The resulting CSV file looks like this:

🌍 Related Tutorial: Understanding the DataFrame df.to_csv() method