Opening fixed-width files
You can use pd.read_fwf
to open fixed-width files in pandas, but there are a lot of different options to pass it and it gets overwhelming quickly. I find that using widths
is usually easiest.
Example
Let’s say we have a file of medieval jobs:
name title salary
Ryxlar Chief Dragon Slayer20000
Tiqla Assistant Alchemist4000
Brynz Brute Squad 1000
Mr PotatoMess Cook 35000
Usually the documentation lists the start and end of each column, but the whole half-open interval thing is tough to work with, so just compute the width of each. You can even count the spaces if you have to!
Then you just make a well-commented list and send it to read_fwf
.
widths = [
9, #name
19, #title
6, #salary
]
df = pd.read_fwf("fixed_width.txt", widths=widths)
df
name | title | salary | |
---|---|---|---|
0 | Ryxlar | Chief Dragon Slayer | 20000 |
1 | Tiqla | Assistant Alchemist | 4000 |
2 | Brynz | Brute Squad | 1000 |
3 | Mr Potato | Mess Cook | 35000 |
And there you go!