Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read CSV file with cell that has multiple lines using C#

Tags:

c#

csv

I am trying to read CSV file that has cell with multiple rows inside it.

This is how the CSV looks like:
enter image description here

row 1, column 'Detail' has multiple lines.

When I am trying to read it using ReadLine() method:

private void buttonBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = "Excel Worksheets|*.csv";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                String filePathAndName = openFileDialog.FileName;
                StreamReader reader = new StreamReader(filePathAndName);
                String line = reader.ReadLine();
                Console.WriteLine(line);
                do
                {
                     line = reader.ReadLine();
                     Console.WriteLine(line);
                } while (line != null);
            }
        }

it splits the cell with the multiple rows to number of rows:

[1]"Time of Day","Process Name","PID","Operation","Path","Result","Detail","Image Path"
[2]"22:52:24.2905182","notepad.exe","4828","Process Start","","SUCCESS","Parent PID: 2484, Command line: ""C:\Windows\system32\notepad.exe"" , Current directory: C:\Users\User\, Environment: 
[3];    =::=::\
[4];    ALLUSERSPROFILE=C:\ProgramData
[5];    APPDATA=C:\Users\User\AppData\Roaming
[6];    asl.log=Destination=file
[7];    CommonProgramFiles=C:\Program Files\Common Files
...

"22:52:24.2905201","notepad.exe","4828","Thread Create","","SUCCESS","Thread ID: 8008","C:\Windows\system32\notepad.exe"
"22:52:24.2915842","notepad.exe","4828","Load Image","C:\Windows\System32\notepad.exe","SUCCESS","Image Base: 0x6f0000, Image Size: 0x30000","C:\Windows\system32\notepad.exe"

in the above logs rows 2-7 should be one row.

I want to read it like powershell did it nicely here using import-csv function:
enter image description here

And you can easily pull data from specific cell by its row and column using the command (example):

$csvContent[0] |select -expand Detail

Example:
enter image description here

like image 920
E235 Avatar asked Nov 07 '14 19:11

E235


People also ask

Can CSV have multiple lines?

In order to process the CSV file with values in rows scattered across multiple lines, use option("multiLine",true) . This yields below output. Note: By default, using the multiline option; Spark considers you have multiline rows in double-quotes or any special escape characters.

How do I handle line breaks in a CSV file?

Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.

What is stored in each line of a csv file?

CSV source file format. Comma-separated values (CSV) files store tabular data in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas (or other characters).


1 Answers

Instead of manually reading in the lines, you can use a library like CsvHelper, which will remove a lot of the headache from parsing a csv.

like image 67
Ryan Kohn Avatar answered Oct 07 '22 12:10

Ryan Kohn