csv-enumerator-0.8.2: A flexible, fast, enumerator-based CSV parser library for Haskell.

Data.CSV.Enumerator

Contents

Synopsis

CSV Data types

type Row = [Field]Source

class CSVeable r whereSource

Methods

rowToStr :: CSVSettings -> r -> ByteStringSource

Convert a CSV row into strict ByteString equivalent.

fileHeaders :: [r] -> Maybe RowSource

Possibly return headers for a list of rows.

iterCSV :: CSVSettings -> CSVAction r a -> a -> Iteratee ByteString IO aSource

The raw iteratee to process any Enumerator stream

fileSink :: CSVSettings -> FilePath -> (Maybe Handle, Int) -> ParsedRow r -> Iteratee ByteString IO (Maybe Handle, Int)Source

Iteratee to push rows into a given file

foldCSVFileSource

Arguments

:: FilePath

File to open as a CSV file

-> CSVSettings

CSV settings to use on the input file

-> CSVAction r a

Fold action

-> a

Initial accumulator

-> IO (Either SomeException a)

Error or the resulting accumulator

Open & fold over the CSV file. Processing starts on row 2 for MapRow instance to use first row as column headers.

mapCSVFileSource

Arguments

:: FilePath

Input file

-> CSVSettings

CSV Settings

-> (r -> [r])

A function to map a row onto rows

-> FilePath

Output file

-> IO (Either SomeException Int)

Number of rows processed

Take a CSV file, apply function to each of its rows and save the resulting rows into a new file.

Each row is simply a list of fields.

mapCSVFilesSource

Arguments

:: [FilePath]

Input files

-> CSVSettings

CSV Settings

-> (r -> [r])

A function to map a row onto rows

-> FilePath

Output file

-> IO (Either SomeException Int)

Number of rows processed

Like mapCSVFile but operates on multiple files pouring results into a single file.

Instances

data CSVeable r => ParsedRow r Source

A datatype that incorporates the signaling of parsing status to the user-developed iteratee.

We need this because some iteratees do interleaved IO (such as outputting to a file via a handle inside the accumulator) and some final actions may need to be taken upon encountering EOF (such as closing the interleaved handle).

Use this datatype when developing iteratees for use with fold* family of functions (Row enumarators).

Constructors

ParsedRow (Maybe r) 
EOF 

CSV Setttings

data CSVSettings Source

Settings for a CSV file. This library is intended to be flexible and offer a way to process the majority of text data files out there.

Constructors

CSVS 

Fields

csvSep :: Char

Separator character to be used in between fields

csvQuoteChar :: Maybe Char

Quote character that may sometimes be present around fields. If Nothing is given, the library will never expect quotation even if it is present.

csvOutputQuoteChar :: Maybe Char

Quote character that should be used in the output.

csvOutputColSep :: Char

Field separator that should be used in the output.

defCSVSettings :: CSVSettingsSource

Default settings for a CSV file.

 csvSep = ','
 csvQuoteChar = Just '"'
 csvOutputQuoteChar = Just '"'
 csvOutputColSep = ','

Reading / Writing CSV Files

readCSVFileSource

Arguments

:: CSVeable r 
=> CSVSettings

CSV settings

-> FilePath

FilePath

-> IO (Either SomeException [r])

Collected data

writeCSVFileSource

Arguments

:: CSVeable r 
=> CSVSettings

CSV settings

-> FilePath

Target file path

-> [r]

Data to be output

-> IO Int

Number of rows written

appendCSVFileSource

Arguments

:: CSVeable r 
=> CSVSettings

CSV settings

-> FilePath

Target file path

-> [r]

Data to be output

-> IO Int

Number of rows written

Folding Over CSV Files

These enumerators generalize the map* family of functions with a running accumulator.

type CSVAction r a = a -> ParsedRow r -> Iteratee ByteString IO aSource

An iteratee that processes each row of a CSV file and updates the accumulator.

You would implement one of these to use with the foldCSVFile function.

funToIter :: CSVeable r => (a -> ParsedRow r -> a) -> CSVAction r aSource

Convenience converter for fold step functions that are pure.

Use this if you don't want to deal with Iteratees when writing your fold functions.

funToIterIO :: CSVeable r => (a -> ParsedRow r -> IO a) -> CSVAction r aSource

Convenience converter for fold step functions that live in the IO monad.

Use this if you don't want to deal with Iteratees when writing your fold functions.

Primitive Iteratees

collectRows :: CSVeable r => CSVAction r [r]Source

Just collect all rows into an array. This will cancel out the incremental nature of this library.

Other Utilities

outputRow :: CSVeable r => CSVSettings -> Handle -> r -> IO ()Source

Output given row into given handle

outputColumns :: CSVSettings -> Handle -> [ByteString] -> MapRow -> IO ()Source

Expand or contract the given MapRow to contain exactly the given set of columns and then write the row into the given Handle.

This is helpful in filtering the columns or perhaps combining a number of files that don't have the same columns.

Missing columns will be left empty.