Code Monkey home page Code Monkey logo

Comments (3)

22222 avatar 22222 commented on September 2, 2024

I purposely left that out of this library because I didn't have any good use cases for it. What use did you have in mind for that method? I wouldn't necessarily be opposed to adding it if there's a good case I haven't thought of.

Otherwise, I can think of a couple alternative ways to get similar functionality that may or may not work depending on your needs.

One option would be to keep a copy of your TextReader and call that when you want to skip a line:

using (var csvReader = new StringReader(csvInput))
using (var parser = new NotVisualBasic.FileIO.CsvTextFieldParser(csvReader))
{
	var fields = parser.ReadFields();
	var line = csvReader.ReadLine();
}

But if you go with that route, be careful of calling the HasNextLine method on the parser since that will read ahead one line and keep a copy of it. And this implementation isn't necessarily guaranteed for future versions of the library, so this maybe isn't an ideal solution.

Another option would be to create an extension method that recreates the line from the parsed fields:

public static class CsvTextFieldParserExtensions
{
	public static string ReadLine(this NotVisualBasic.FileIO.CsvTextFieldParser parser)
	{
		var fields = parser.ReadFields();
		if (fields == null) return null;
		
		return string.Join(",", fields.Select(SerializeValue));
	}

	private static string SerializeValue(string value)
	{
		if (value == null) return null;

		if (value.IndexOfAny(new char[] { ',', '"', '\n', '\r' }) >= 0)
		{
			return '"' + value.Replace("\"", "\"\"") + '"';
		}
		return value;
	}
}

Then you could call ReadLine with the same signature as the VB parser:

using (var csvReader = new StringReader(csvInput))
using (var parser = new NotVisualBasic.FileIO.CsvTextFieldParser(csvReader))
{
	string line;
	while ((line = parser.ReadLine()) != null)
	{
		yield return line;
	}
}

But this would not work exactly like the VB version if any of the field values contain an end-of-line character. The VB version would read just until the first end-of-line character (stopping in the middle of the field), while this version would read the entire CSV row. So that could be good or bad depending on what you're looking for. It also wouldn't necessarily work correctly if you use custom delimiters or other options, although the extension method could probably be enhanced to read those custom values from the parser.

And there's always the option of just copying the source code into your own project and adding the method yourself.

from csvtextfieldparser.

Krakean avatar Krakean commented on September 2, 2024

@22222
Well, to be honest I dont know about use case. But it doesn't allow me to use your CsvTextFieldParser as drop-in replacement for original TextFieldParser.
I have some legacy code, which uses TextFieldParser, and use ReadLine() like this:
...
string inLine = parser.ReadLine();
string[] items = inLine.Split(',');
myItems = items;

bool toKeep = MyProcessLine(exchange,
inLine, items, filePrefix, fileType,
myCommodityCodesLookup, commodityGroupLookup,
ref isRecordType30Passed,
ref isRecordType40Passed,
ref isRecordType50Passed);
if (toKeep)
{
if (inLine != "")
{
myOutFile.WriteLine(inLine);
}
}
...

And there is a lot of places similar to this.
Would be very nice if you can add ReadLine() like it was in original TextFieldParser, and let user decide the use case for it :)
In my case I wont use it, but I just need to keep legacy code compile with your CsvTextFieldParser as drop-in replacement.

And, thanks for your CsvTextFieldParser.

from csvtextfieldparser.

22222 avatar 22222 commented on September 2, 2024

Thanks for the example. In this case, I'd agree that you wouldn't necessarily want to use this library. It seems like you'd be better off just using a TextReader (such as StringReader) directly.

The real VB TextFieldParser handles more than just CSV files, so this library was intended to be a subset with just parts needed for parsing CSV files. And another goal is keeping this thing as simple as possible. That ReadLine method doesn't really have anything to do with parsing CSV files, so that's why it was left out.

So I probably won't add it at this time. But feel free to directly include the source code of the parser in your own project and add the method in if you decide you need it. All of the code is in a single file to make that as easy as possible.

from csvtextfieldparser.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.