-
-
Notifications
You must be signed in to change notification settings - Fork 320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trim quoted values when importing csv? #1051
Comments
It does some of that already, but I guess it would be good to do a bit more here. Thanks for the example. Would you be interested in having a look at the Hledger.Read.CsvReader code ? The strip function would do the job. |
Hmmm. Haskell a bit of a mind bender. After fighting with types and fmap for a bit have lazily used this for the moment:
|
Cool. It definitely is at first. If you feel like taking another whack at it, help is available in #hledger. I'll recommend |
Though, thinking about it a little more.. space is significant in CSV, so cassava is right to return it to us. The problem is our field assignment combining a minus sign and a CSV value, which is a fragile way to generate a parseable number. Interpolating a CSV value happens in renderTemplate. If we called strip there, this should work ? However maybe it's not appropriate to strip every interpolated CSV field this way.. And separately, I have become confused. My local repro has been:
which has no interpolation rule, and fails earlier than yours, while cassava is parsing the CSV. I don't understand how yours gets as far as it does. |
Oh yea, I see my error. |
Yes, I was thinking of #1037. Back to your issue: I confirm that adding strip like so fixes it:
Now, is it acceptably intuitive/predictable/principled that %-interpolated values would always have whitespace stripped.. |
Actually there's a separate code path when assigning an amount field ( |
Sorry for the stream-of-thought. Rather, it would be: "Any CSV value interpolated in a Which sounds a bit messy. |
This removes a potential snag in amount field assignments, and hopefully is harmless and acceptable otherwise.
I just went ahead and committed "always strip outer whitespace when interpolating". This removes the snag you hit and will hopefully be harmless otherwise. I'll mention it on list. |
Hey - thanks. And for the tip on ghci too - that makes debugging etc much easier! I'll try and have a go if something else comes up. I've been interested in Haskell for a while. |
For the record: RFC 4180 2.4 says "Spaces are considered part of a field and should not be ignored", so we're going slightly against the CSV spec here. Perhaps better if we would strip spaces only from amounts, but we're not aware of CSV fields' meanings when interpolating. |
I am importing some CSV from Amex and noticed that their csv amount field is double-quoted and includes a space in front of the value. So, for example:
" -140.00"
.I noticed that this causes an error when trying to invert the amount in the csv rules file. So if I add the line:
amount -%amount GBP
it then throws and error on import.In full, the output is:
Deleting the space in the input csv file removes the error.
Is there a way to force hledger to trim incoming values?
The text was updated successfully, but these errors were encountered: