Skip to content

Instantly share code, notes, and snippets.

@dmjio
Created October 30, 2019 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmjio/8477be4a5acb2106676aae03f2fffc02 to your computer and use it in GitHub Desktop.
Save dmjio/8477be4a5acb2106676aae03f2fffc02 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import Control.Monad
import qualified Data.ByteString.Lazy.Char8 as BL8
import qualified Data.Csv as CSV
import Data.List
import qualified Data.Vector as V
-- main :: IO ()
-- main = do
-- bytes <- BL8.readFile "file.csv"
-- let xs = chuns 34 (BL8.split ',' bytes)
-- mapM_ (print . length) xs
-- print (last xs)
chunks :: Int -> [a] -> [[a]]
chunks _ [] = []
chunks n xs = do
case splitAt n xs of
(ls,rs) -> ls : chunks n rs
-- cols :: Int
-- cols = do
-- length ["Superfamily/Family","Account Number","Trading Currency Code","Account (Account Number)","Superfamily/Family Name","Issue Currency Description(Currency Code)","Trading Currency Description","Entry Date","Trade Date","Settle Date","Transaction ID","Transaction Type","Executing Broker Code","Transaction Comments","Security ID","Security Description","Quantity","Trade Price","Transaction Exchange Rate","Accrued Interest Purchased/Sold","Net Money","CUSIP","Floor Commissions","CMTA Commissions","PB Commissions","Total Commissions","Option Regulatory Fees","SEC Fees","Fees Postage","Fees Service","Fees Other","Total Fees","Dividend Rate","Bloomberg Ticker"]
main :: IO ()
main = do
bytes <- BL8.lines <$> BL8.readFile "lol.csv"
let chunked = fmap (BL8.split ',') bytes
result = groupBy comparator chunked
header = BL8.intercalate "," $ BL8.split ',' $ head $ fmap (BL8.intercalate ",") (result !! 0)
forM_ result $ \chunkedGroup -> do
let fileName = BL8.unpack $ BL8.filter (/='-') ((chunkedGroup !! 0) !! 7)
result = (BL8.filter (/='"') header) <> "\n" <> do
BL8.intercalate "\n" $ (fmap (BL8.intercalate ",") chunkedGroup)
BL8.writeFile (fileName <> ".csv") (result <> "\n")
putStrLn "done"
comparator :: [BL8.ByteString] -> [BL8.ByteString] -> Bool
comparator l r = l !! 8 == r !! 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment