Skip to content

Instantly share code, notes, and snippets.

@mistidoi
Created July 25, 2016 00:30
Show Gist options
  • Save mistidoi/e2a4404ac277cdc754df417d90a49e04 to your computer and use it in GitHub Desktop.
Save mistidoi/e2a4404ac277cdc754df417d90a49e04 to your computer and use it in GitHub Desktop.
bsp2csv in Haskell
import System.Environment
import Data.List.Split
import Data.List.Utils
import Data.List
-- usage: $ ./bcp2csv input.txt output.txt
main :: IO ()
main = do
args <- getArgs
input <- readFile $ head args
let outputFilename = args !! 1
writeFile outputFilename . unlines . map joinWithCommas $ parseAndEscapeBCP input
-- runs the escaping code on the nested list data structure generated by parseBCP.
parseAndEscapeBCP :: String -> [[String]]
parseAndEscapeBCP input = map (map (quoteIfNecessary . escapeQuotes)) $ parseBCP input
-- parses BCP file into nested Lists. top level is lines, next level is fields.
parseBCP :: String -> [[String]]
parseBCP input = map splitFields $ splitLines input
splitLines :: String -> [String]
splitLines input = splitOn "&$&" input
splitFields :: String -> [String]
splitFields input = splitOn "#&#" input
escapeQuotes :: String -> String
escapeQuotes input
| '"' `elem` input = replace "\"" "\"\"\"" input
| otherwise = input
quoteIfNecessary :: String -> String
quoteIfNecessary input
| '\n' `elem` input || ',' `elem` input || '"' `elem` input = wrapInQuotes input
| otherwise = input
wrapInQuotes :: String -> String
wrapInQuotes input = "\"" ++ input ++ "\""
joinWithCommas :: [String] -> String
joinWithCommas list = intercalate "," list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment