Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading .dat file with fixed column width

The code I use in SAS

Options symbolgen ps=10000;
Data span_nonspan;
INFILE 'C:\September 2016\SAMPLE.dat'; 
   INPUT @1     XYZ       $10.
         @11    ABC       $7.         
         @18    PM        $3.
run;

Can anyone please help me how to write in Python 3.5 to read data same way, I am new to Python and any help would be really appreciated.

The data would look something like below:

5085489966001001600220161002191219    1P  106SCHWARTZ
like image 941
Ramesh Dharmegowda Avatar asked Aug 14 '26 02:08

Ramesh Dharmegowda


1 Answers

import pandas as p
data = p.read_fwf("filename", colspecs=[(0,10),(11,17),(17,20),(20,24)],names=['DP','PHONETYPE','MARKET','FOLDER'])
data.head()
like image 108
Ramesh Dharmegowda Avatar answered Aug 16 '26 21:08

Ramesh Dharmegowda