quoted text errors out #210
-
I have this row in a pipe delimited txt file:
It errors out on this field: "CANADIAN" CIGARETTES there is only a space between "CANADIAN" and CIGARETTES The goal is to read the file, add lineNumber and fileName at the end of each line and write it back to a txt file in another folder so it can be consumed as External Table by SQL Server. But it errors out with this: It doesn't tell me at what line in the text file it happens so had to eliminate records until I find the issue. Is there a way to find the line? Also I don't want any escape characters to be added. Just want the fields to get written as they are. Here is my code (not adding the lineNumber and fileName yet): ` private void button1_Click(object sender, EventArgs e)
`
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Your file is a non-standard format and needs special configuration. The following code should do what you want: using Sylvan.Data;
using Sylvan.Data.Csv;
using System.Data.Common;
var filename = "MyFileName.csv";
// example data
var data = "a|b|c\nx|\"y\" y|z\n";
var opts =
new CsvDataReaderOptions {
CsvStyle = CsvStyle.Escaped,
Escape = '\0',
Quote = '\0',
};
var r = CsvDataReader.Create(new StringReader(data), opts);
// create a new data reader with the filename and row number attached.
DbDataReader r2 = r.WithColumns(
new CustomDataColumn<string>("Filename", _ => filename),
new CustomDataColumn<int>("Row", _ => r.RowNumber)
);
var wo = new CsvDataWriterOptions
{
Style = CsvStyle.Escaped,
Escape = '\0',
Quote = '\0',
Delimiter = '|',
};
using var x = CsvDataWriter.Create("dump.csv", wo);
x.Write(r2);
|
Beta Was this translation helpful? Give feedback.
-
I do have Sylvan.Data; directive but it is greyed out by VS and when I hover over it it says "IDE0005: Using directive is unnecessary". This doesn't seem an error. |
Beta Was this translation helpful? Give feedback.
-
you were right. I didn't have Sylvan.Data added in Nuget :-( Thank you for quick replies! |
Beta Was this translation helpful? Give feedback.
Your file is a non-standard format and needs special configuration.
The following code should do what you want: