In this recipe, we will parse a fixed-width file by creating a simple streaming parser. Our sample file will contain fixed-width columns of strings, numbers, dates, and Boolean values of accounting data. Our goal is to count the records and get a total credit and debit amount for all records that have been posted.
Our sample file looks like the following:
In this section, we will create our own parser for fixed-width files. When considering the features we would like for our basic parser, it should:
Work with a stream reader to allow parsing very large data files
Use a schema to define our column names, positions, sizes, and data types
Parse data to native types
We will use Node's built-in stream reader to read our data files.
We will create our schema as an array of column objects. Each column has a name
, starting position (start
), size
, and type
. Supported type values include string
, float
, date
, and Boolean
.
Because of the variety of date and number formats...