From eff1b31c86892958e7ddd2926dacade9103118a4 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 2 Oct 2019 14:48:51 -1000 Subject: [PATCH] csv: allow csv records with varying lengths, padding with empties Sometimes trailing empty fields are omitted entirely (including the commas) in CSV records. (I see this in exported Google spreadsheets.) Now we don't raise an error in this case, instead we automatically pad any "short" records with empty fields. Not yet well tested. --- hledger-lib/Hledger/Read/CsvReader.hs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index e520170fe..f62a02c99 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -222,14 +222,17 @@ validateCsv numhdrlines (Right rs) = validate $ drop numhdrlines $ filternulls r where filternulls = filter (/=[""]) validate [] = Right [] - validate rs@(first:_) - | isJust lessthan2 = let r = fromJust lessthan2 in Left $ printf "CSV record %s has less than two fields" (show r) - | isJust different = let r = fromJust different in Left $ printf "the first CSV record %s has %d fields but %s has %d" (show first) length1 (show r) (length r) + validate rs@(_first:_) + | isJust lessthan2 = let r = fromJust lessthan2 in + Left $ printf "CSV record %s has less than two fields" (show r) + -- | isJust different = let r = fromJust different in + -- Left $ printf "the first CSV record %s has %d fields but %s has %d" + -- (show first) length1 (show r) (length r) | otherwise = Right rs where - length1 = length first lessthan2 = headMay $ filter ((<2).length) rs - different = headMay $ filter ((/=length1).length) rs + -- length1 = length first + -- different = headMay $ filter ((/=length1).length) rs -- -- | The highest (0-based) field index referenced in the field -- -- definitions, or -1 if no fields are defined.