A delimiter separates fields within each record of a delimited text file. Although CSV commonly means comma-separated values, the field delimiter can be another character, provided the same delimiter is used throughout the file. section 7.1
When parsing delimited records, account for:
- Line separators divide records, while field separators divide the values within each record. section 8.1
- Parsing options should make the line separator and field separator configurable; the default field separator is a comma. section 8.1
- A simple parser can split text into lines, split each line into fields, and check that records contain matching field counts. section 8.1
- This treatment skips empty lines and deliberately leaves quoted fields and escaped double quotes unsupported. section 8.1
Inspecting a CSV file
A cities.csv file uses commas between fields and newlines between records. A command can enable header handling, search for “Ya,” and select the City and State fields to display Yankton and Yakima. section 7.1
Listing 8.1 Options for CSV file parsing
data Separators = Separators #1
{ sepLineSeparator :: Char,
sepFieldSeparator :: Char
}
deriving (Eq, Show)
data HeaderOption = WithHeader | WithoutHeader #2
deriving (Eq, Show)
data CsvParseOptions = CsvParseOptions #3
{ cpoSeparators :: Separators,
cpoHeaderOption :: HeaderOption
}
deriving (Eq, Show)
defaultSeparators :: Separators
defaultSeparators =
Separators #4
{ sepLineSeparator = '\n',
sepFieldSeparator = ','
}
defaultOptions :: CsvParseOptions
defaultOptions =
CsvParseOptions #5
{ cpoSeparators = defaultSeparators,
cpoHeaderOption = WithoutHeader
}
Browse the Haskell data types that configure line and field separators, header handling, and comma-based defaults. section 8.1










