The fwf Module

A reader for fixed width files (FWF) produced by SAS providing the same interface as CSV reader.

Each record is returned in a way similar to the standard CSV reader, either as a list of objects or as a dictionary, where keys are column headers.

To read the file, the application mus supply a list of columns. If a file is produced by SAS the columns are usually described in File Transfer Summary (FTS) file. However, FTS files are non-standard, therefore a universal parser for FTS files is not feasible to supply.

class FWFColumn(order: int, name: str, type: str, start: int, width: Tuple[int, int])[source]

A class describing a column of a fixed width (FWF) file

name

Name (header) of the column

type

Type of the column. Supported types: NUM, CHAR, DATE

ord

Ordinal number for the column

start

Starting position for the column (in bytes)

length

Length of the column (in bytes)

end

Ending position for the column (in bytes)

d

Scale of the column for floating point numbers

class FWFMeta(path: str, record_len: int, columns: List[FWFColumn], number_of_rows=None, size=None)[source]

Metadata for the FWF file

rlen

Record length, i.e. the length in bytes of the block for one record

ncol

Number of columns

nrows

Number of rows, if given in the file descriptor (can be None)

size

File size in bytes

path

Physical path to the file on the file system

columns

A list of FWFColumn instances, describing each column

class FWFReader(meta: FWFMeta, ret_dict: bool = False)[source]

Fixed width files (FWF) reader. Returns records in the same way as standard CSV reader. Can return each record as either a list of values or a dictionary with column headers as keys.

Constructor

Parameters:
  • meta – an instance of FWFMeta

  • ret_dict – boolean value, denoting whether to return each record as a dictionary

good_lines

Number of successfully read records

bad_lines

Number of records that have failed parsing

column_names() List[str][source]
open()[source]
close()[source]
validate()[source]
read_record()[source]
next()[source]
on_parse_exception()[source]
exception FTSParseException(msg: str, pos: int)[source]

Exception raised when a record cannot be parsed