D Allan
D Allan

Reputation: 11

ValueError when reading in fixed width file using pd.read_fwf - number of expected fields not matching number seen

My current code contains the following:

columns=[(0,4), (4,8), (8,9), (9,10), (20,22), (23,24)]

header=['var1','var2','var3','var4','var5','var6']

file=pd.read_fwf('file_name.gz', compression='gzip', colspec=columns, names=header)

When I run I get the following : ValueError: Expected 8 fields in line 1, saw 3

The data contained in the input file looks as follows:

02011602160108 26 312870000

It seems to be reading the white space instead of noting the column specs

Upvotes: 1

Views: 206

Answers (1)

piRSquared
piRSquared

Reputation: 294358

as @StephenRauch stated in his comment (while I was sluggishly compiling this answer)

from io import StringIO
import pandas as pd

txt = """02011602160108 26 312870000"""

columns=[(0,4), (4,8), (8,9), (9,10), (20,22), (23,24)]
header=['var1','var2','var3','var4','var5','var6']

pd.read_fwf(StringIO(txt), colspecs=columns, names=header)

   var1  var2  var3  var4  var5  var6
0   201  1602     1     6    28     0

Upvotes: 1

Related Questions