1

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

1
  • 3
    keyword is colspecs, ie. plural Commented Mar 20, 2017 at 21:00

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.