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

encoding/csv: getting a line number of current record when using csv.Reader #26679

Closed
alser777 opened this issue Jul 29, 2018 · 8 comments
Closed
Labels
FeatureRequest FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@alser777
Copy link

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.10.3

Does this issue reproduce with the latest release?

1.10.3 is the latest

What did you do?

I have used csv.Reader to parse a CSV file and I wanted to report an error to user in case some record contained invalid data. I also wanted to include a line number of invalid record in an error message.

What did you expect to see?

some method that would return a line number where current record starts

What did you see instead?

There is no way to get a line number of current record.

@alser777 alser777 changed the title Getting a line number of current record when using csv.Reader csv: getting a line number of current record when using csv.Reader Jul 29, 2018
@alser777 alser777 changed the title csv: getting a line number of current record when using csv.Reader encoding/csv: getting a line number of current record when using csv.Reader Jul 29, 2018
@agnivade
Copy link
Contributor

Is there any issue using a counter variable to keep track of the no. of lines parsed ?

@nussjustin
Copy link
Contributor

@agnivade Probably, since a single record can span multiple lines. E.g. the 3rd record could be from line 7 to 20 in the source.

The Reader only tracks the current line not the line where a record started. We could add a method that returns the current line number, but to get the line where the last record started we would need to change the Reader to remember the line.

@agnivade
Copy link
Contributor

/cc @dsnet

@agnivade agnivade added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. FeatureRequest labels Jul 30, 2018
@agnivade agnivade added this to the Unplanned milestone Jul 30, 2018
@dsnet
Copy link
Member

dsnet commented Jul 30, 2018

What's wrong with the current information provided:

type ParseError struct {
        StartLine int   // Line where the record starts
        Line      int   // Line where the error occurred
        Column    int   // Column (rune index) where the error occurred
        Err       error // The actual error
}

Isn't ParseError.StartLine exactly what is being asked for?

@alser777
Copy link
Author

@dsnet No, I ask for a method to get StartLine when there is no parse error

@dsnet
Copy link
Member

dsnet commented Jul 30, 2018

Ah, I see. This seems to be a relatively niche feature, and I'm not sure if the expansion of API is worth it.

Something along the lines of what @agnivade suggested should work. The issue of multi-line records can be resolved by sampling the current line before each call to csv.Reader.Read. However, this is complicated by the fact that csv.Reader always wraps the input reader with a bufio.Reader regardless of whether the input is already a bufio.Reader or not. Thus, any external accounting is invalidated by the internal buffering of the csv.Reader.

If we changed the internal implementation of csv.Reader to avoid re-wrapping a bufio.Reader, you could account for this yourself by creating a io.Reader that counts the number of lines subtracted by the number of newlines in the bufio.Reader buffer (see Peek).

@agnivade
Copy link
Contributor

@dsnet - It is a bit unclear on what is to be done here. Are you suggesting we should check the reader and avoid the re-wrap ?

@agnivade
Copy link
Contributor

ping @dsnet .

@golang golang locked and limited conversation to collaborators Sep 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants