-
Notifications
You must be signed in to change notification settings - Fork 74
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
Decoding CSV file with CRLF line endings fail with error if the last column is quoted #35
Comments
Hi @xsleonard, I am out for the weekend so I don't think I will manage to solve this till Tuesday. Your bug description seems clear and I believe I understand the problem, but I would appreciate if you could provide a CSV with two or three rows (of dummy data) triggering the problem. That way I can even include it as a test so it doesn't happen again. |
Ok, I'll look into more on Monday as well. |
Using a trimStrategy of A more intuitive solution would accept multicharacter Delimiter.Row (that's what I was doing before, |
Hey @xsleonard. I built some tests and tried your file and everything worked. extension DecodingBadInputTests {
/// Tests CRLF delimiters with escape fields.
func testFileCRLF() throws {
let decoder = CSVDecoder {
$0.encoding = .utf8
$0.bufferingStrategy = .sequential
$0.headerStrategy = .firstLine
$0.trimStrategy = .whitespaces
$0.delimiters.row = "\r\n"
}
let file = try decoder.decode([[String]].self, from: URL(fileURLWithPath: "/Users/marcos/Desktop/test.csv"))
XCTAssertEqual(1, file.count)
XCTAssertEqual(["G"], file[0])
}
/// Tests CRLF delimiters with escape fields.
func testLazyFileCRLF() throws {
let decoder = try CSVDecoder {
$0.encoding = .utf8
$0.bufferingStrategy = .sequential
$0.headerStrategy = .firstLine
$0.trimStrategy = .whitespaces
$0.delimiters.row = "\r\n"
}.lazy(from: URL(fileURLWithPath: "/Users/marcos/Desktop/test.csv"))
XCTAssertEqual(["G"], try decoder.decodeRow([String].self))
XCTAssertNil(decoder.next())
}
} I also recreated the file in code in case you sent me an invalid file, but it also worked. extension DecodingBadInputTests {
/// Tests CRLF delimiters with escape fields.
func testInputCRLF() throws {
let input = "Name\r\n\"G\"\r\n"
let decoder = CSVDecoder {
$0.encoding = .utf8
$0.bufferingStrategy = .sequential
$0.headerStrategy = .firstLine
$0.trimStrategy = .whitespaces
$0.delimiters.row = "\r\n"
}
let file = try decoder.decode([String].self, from: input)
for row in file { print(row) }
}
} Are you sure the problem is not on your side. Maybe bad file data? |
Sorry, I described/diagnosed the problem wrong. If you change The error ( The solution to parsing both CRLF and LF files is using a trimStrategy containing I suggest one of these:
|
You are right. The error message doesn't provide correct information. I have changed it adding more useful data and push to master. Regarding changing the default (which is the same problem you brought in #33): It has some side-effects, since \r may be meaningful to some users and they may want that data in their fields. That is why I make the user to explicitly change the configuration values. I agree is not ideal and I will probably add some information in the README about CLRF files. |
I wouldn't make the |
I agree that having a special row delimiter that cover both |
This functionality is now supported by |
Describe the bug
A clear and concise description of what the bug is.
Decoding a CSV file with CRLF line endings fails with an error, if the last field in a row is quoted.
The error:
To Reproduce
Steps to reproduce the behavior:
Using a CSV file with CRLF line endings (
url
), decode with this:Expected behavior
No error
System
Additional context
This was introduced in v0.6.6
The text was updated successfully, but these errors were encountered: