I am trying to read CSV file that has cell with multiple rows inside it.
This is how the CSV looks like:
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:
And you can easily pull data from specific cell by its row and column using the command (example):
$csvContent[0] |select -expand Detail
Example:
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.
Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With