question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parse CSV to String Array

See original GitHub issue

We have a use case where we are dynamically generating csv columns and don’t know what columns are available until runtime. Therefore, we can’t define a CSVMapping object in order to instantiate a CSVParser object.

I would like the ability to define and CSVParser without a mapping object and parse a file/string into a String Array. Something like… String[] sArray = new CsvParser(csvParserOptions).ParseToStringArray(filePath);

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:12

github_iconTop GitHub Comments

3reactions
bytefishcommented, May 25, 2019

@alexb5dh @hthroweriii I have released a Version, which now allows to parse to String Arrays and use the existing infrastructure. Many, many thanks to you two for the input! And sorry to @hthroweriii, I didn’t really understand your initial request and closed the issue too early.

You can now use it like this:

[Test]
public void StringArrayMappingTest()
{
    CsvParserOptions csvParserOptions = new CsvParserOptions(false, ';');
    CsvReaderOptions csvReaderOptions = new CsvReaderOptions(new[] { Environment.NewLine });
    CsvStringArrayMapping csvMapper = new CsvStringArrayMapping();
    CsvParser<string[]> csvParser = new CsvParser<string[]>(csvParserOptions, csvMapper);

    var stringBuilder = new StringBuilder()
        .AppendLine("Philipp;Wagner;1986/05/12")
        .AppendLine("Max;Mustermann;2014/01/01");

    var result = csvParser
        .ReadFromString(csvReaderOptions, stringBuilder.ToString())
        .ToList();

    Assert.AreEqual(2, result.Count);

    Assert.IsTrue(result.All(x => x.IsValid));

    Assert.AreEqual("Philipp", result[0].Result[0]);
    Assert.AreEqual("Wagner", result[0].Result[1]);
    Assert.AreEqual("1986/05/12", result[0].Result[2]);

    Assert.AreEqual("Max", result[1].Result[0]);
    Assert.AreEqual("Mustermann", result[1].Result[1]);
    Assert.AreEqual("2014/01/01", result[1].Result[2]);
}
1reaction
alexb5dhcommented, May 25, 2019

Thanks @bytefish, looks great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to parse CSV to Array in JavaScript? [SOLVED]
Method-3: Use csv-parse method ... If we want to convert CSV to an array locally, we can use a Node.js package called csv-parse...
Read more >
Convert a Python csv string to array
In this article, we are going to find out how to convert a CSV string into an array. The first approach is by...
Read more >
JavaScript - Parse CSV data into an array
To convert or parse CSV data into an array , you need to use JavaScript's FileReader class, which contains a method called readAsText()...
Read more >
How can I convert a comma-separated string to an array?
The easiest solution (and more secure I bet) was to use PapaParse which has a "no-header" option that transform the CSV file into...
Read more >
JavaScript - CSV to array
Converts a comma-separated values (CSV) string to a 2D array. Use Array.prototype.indexOf() to find the first newline character ( \n ).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found