Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go back in parser rows

Tags:

c#

parsing

I'm trying to parse a csv file with C# (using the Microsoft.VisualBasic.FileIO library) Although I can parse row fields using this method

fields = parser.ReadFields();

I would need to "go back" and read the same line again with this method:

 parser.ReadLine()

Is there a method to go back and read again the same line without parsing the whole file again?

like image 251
valbrux Avatar asked Jan 21 '26 01:01

valbrux


1 Answers

That's a common problem with fast forward-only parsers - you can't go back to check the content that caused a parsing error. That's why they are fast in the first place.

TextFieldParser raises a MalformedLineException in case of parsing errors. You can catch it and check the LineNumber property, or check the parser's ErrorLine and ErrorLineNumber properties.

In case of semantic errors, ie if the fields don't make sense, you'll have to use the input stream itself to find and load the original line.

If you use the TextFieldParser constructor that accepts a Stream object, you can keep track of the stream position befor and after each call to ReadFields(). If you detect a semantic error, you could change the stream's position to the one just before ReadFields and call TextFieldParser.ReadLine(), or just read a line from the stream with a StreamReader. For example:

try
{
    ...
    int pos=myStream.Position;
    var fields=parser.ReadFields();
    if (!fieldsOK(fields))
    {
        myStream.Seek(pos);
        var errorLine=parser.ReadLine();
        //Log it
    }
}
catch(MalformedLineException exc)
{
    var errorLine=parser.ErrorLine;
    //Log it
}

If you can't use a stream, or the stream is forward only, you may have to store the CSV data somewhere before parsing and reopen it to read the line. If your input is a file path for example, you can reload the file, move to the error line position and just read a line.

If you expect many semantic errors, it's best to collect error positions in a list so that you can read all relevant lines after you finish parsing the entire file.

like image 66
Panagiotis Kanavos Avatar answered Jan 22 '26 15:01

Panagiotis Kanavos