I want to read in a text file that has a fixed width format. Unfortunately, it also contains a varchar field that tells me the length in the beginning (so not so fixed width after all). The file looks something like this
Boris 1520190730 0014likes icecreamblue
Lena 1320190815 0009is blondered
with a schema that looks something like this:
{
'name':10,
'age':2,
'last_visit':8,
'other_field':5,
'comment':???,
'fav_color':8
}
Before I came across the varchar field, my approach was to read it in either with pandas' read_fwf
or (with a slightly modified schema) via df[col].str[schema[col][0]:schema[col][1]
. This fails of course for the variable length field. At least the field tells me its length at the beginning(0014 and 0009).
Is there an elegant pandas way to read in such a file? Or do I have to loop over it line by line and handle the field dynamically?