我有一个 CSV 文件,有 2 列,每列有 32100 行。我需要导入这些数据并将每列分成一个列表列表,每个列表有一千个值。例如,让column2_data = [[1,2,3,4,], [3,4,6,7], ...](注意:数据是float类型)。”
import Data.Csv
import qualified Data.ByteString.Lazy as BL
import qualified Data.Vector as V
parserCsv :: IO ()
parserCsv = do
csvData <- BL.readFile "src/salary.csv"
case decode NoHeader csvData of
Left err -> putStrLn err
Right v -> V.forM_ v $ \(x, y) ->
putStrLn $ show (y :: Float) ++ " // " ++ show (y :: Float)
I need to import this data and separate each column into a list of lists with a thousand values each list. E.g., let column2_data = [[1,2,3,4,], [3,4,6,7], ...] (Note: the data is of type float).
我会分离逻辑以将其转换为列表,其中:
groupAt :: Int -> [a] -> [[a]]
groupAt n = go
where go [] = []
go xs = ys : go zs
where ~(ys, zs) = splitAt n xs
然后用它来处理解码的结果:
parserCsv :: IO ([[Float]], [[Float]])
parserCsv = do
csvData <- BL.readFile "src/salary.csv"
case decode NoHeader csvData of
Left err -> error err
Right v -> (groupAt 1000 (toList fmap fst v), groupAt 1000 (toList fmap snd v))