Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header does not contain Quote, but some fields in row contain Quote, how should I read this kind of CSV #2250

Closed
Zero-Xiong opened this issue Apr 24, 2024 · 4 comments
Labels

Comments

@Zero-Xiong
Copy link

really need help here, as I tried both Mode = RFC4180 & Mode = Escape

The CSV file content is like below (example):

=======================================================

Header1,Header2,Header3,IntHeader,DecimalHeader,DateTimeHeader
content1,"content , (i have comma inside) 2","content3",100,100.00,"04/23/2024"
content2,"content ,,, (many comma inside)","content2",200,200.00,"05/01/2024"

=======================================================

The problem is header without double quote, field1 does not contain double quote, but field2 contain both double quote and comma inside.

How do we read such CSV file?

@Zero-Xiong Zero-Xiong added the bug label Apr 24, 2024
@JoshClose
Copy link
Owner

Seems to work fine for me with defaults.

void Main()
{
	var s =
		"""
		Header1,Header2,Header3,IntHeader,DecimalHeader,DateTimeHeader
		content1,"content , (i have comma inside) 2","content3",100,100.00,"04/23/2024"
		content2,"content ,,, (many comma inside)","content2",200,200.00,"05/01/2024"
		""";
	var config = new CsvConfiguration(CultureInfo.InvariantCulture)
	{
	};
	using (var reader = new StringReader(s.ToString()))
	using (var csv = new CsvReader(reader, config))
	{
		csv.GetRecords<Foo>().ToList().Dump();
	}
}

public class Foo
{
	public string Header1 { get; set; }
	public string Header2 { get; set; }
	public string Header3 { get; set; }
	public int IntHeader { get; set; }
	public decimal DecimalHeader { get; set; }
	public DateTime DateTimeHeader { get; set; }
}
image

@Zero-Xiong
Copy link
Author

Sign..... really sad! it's not working in my environment.

FYI, my version is 31.0.3

@Zero-Xiong
Copy link
Author

I guess maybe the Unicode character inside the actual CSV file.

like below:

image

@JoshClose
Copy link
Owner

CsvHelper doesn't care about that. You set the encoding in your TextReader. It'll use whatever delimiter you have set up in the config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants