Skip to content
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

CsvFileReader#readAllAsSequence fails on non-equal sized rows #61

Closed
koral-- opened this issue Sep 3, 2020 · 8 comments
Closed

CsvFileReader#readAllAsSequence fails on non-equal sized rows #61

koral-- opened this issue Sep 3, 2020 · 8 comments
Labels
question Further information is requested

Comments

@koral--
Copy link

koral-- commented Sep 3, 2020

Describe the bug
If rows are not equal-sized an exception is thrown:
com.github.doyaaaaaken.kotlincsv.util.CSVFieldNumDifferentException: Fields num seems to be 4 on each row, but on 2th csv row, fields num is 3.

To Reproduce

        csvWriter().open(csvFile) {
            writeRow(listOf("a"))
            writeRow(listOf("a", "b"))
        }
        csvReader().open(csvFile) {
           readAllAsSequence()
        }

Expected behavior
Missing cells are treated as nulls or empty strings.

Environment

  • kotlin-csv version 0.11.0
  • OS: Android 10

Screenshots
N/A

@koral-- koral-- added the bug Something isn't working label Sep 3, 2020
@doyaaaaaken
Copy link
Owner

@koral--
Thanks for reporting!

Throwing exception is expected behavior.
If you change behavior, the skipEmptyLine option on CsvReader is available.
See https://github.com/doyaaaaaken/kotlin-csv#customize

@doyaaaaaken doyaaaaaken added question Further information is requested and removed bug Something isn't working labels Sep 16, 2020
@doyaaaaaken
Copy link
Owner

Any other question on this topic? Feel free to ask.
If not, I'll close this issue.

@koral--
Copy link
Author

koral-- commented Sep 29, 2020

I don't longer need this library, but can help resolve this issue if needed.

I don't understand your explanation. Could you elaborate?
Is it really intended that CSV written by this library is not readable by itself?

@doyaaaaaken
Copy link
Owner

The "CSV" definition is defined on RFC4180.
It says here.

Within the header and each record, there may be one or more fields, separated by commas. Each line should contain the same number of fields throughout the file.

So, your sample code doesn't write "CSV" file, so it's not unnatural that kotlin-csv cannot read your writing file.

@koral--
Copy link
Author

koral-- commented Sep 29, 2020

OK, thanks. Now I understand the explanation.
One little question then: is it intended that library allows writing a file which does not conform to CSV format?
In this particular case the 2nd #writeRow() call may throw an exception (or it can be thrown when closing file, technical details are not important now).

@doyaaaaaken
Copy link
Owner

One little question then: is it intended that library allows writing a file which does not conform to CSV format?

Yes, it's intended.

In this particular case the 2nd #writeRow() call may throw an exception (or it can be thrown when closing file, technical details are not important now).

Yes, it should be. To throw exception on the 2nd writeRow() call is useful.
At this point, I don't plan to implement this validation by myself, but pull requests are welcomed. 👍

@koral--
Copy link
Author

koral-- commented Sep 30, 2020

OK so I guess that this issue can be rephrased to something like "Fail fast on writing malformed CSV" and tagged as enhancement., right?

@doyaaaaaken
Copy link
Owner

Yeah, right. Thank you.
Maybe, it'd be better to create another issue, relate this issue on that, and close this issue.

@koral-- koral-- closed this as completed Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants