CSVHelper 在将文化从 de-DE 更改为 en-US 时引发 HeaderValidationException。

huangapple go评论62阅读模式
英文:

CSVHelper throws HeaderValidationException when changing the culture from de-DE to en-US

问题

I'm trying to read a CSV file using CSVHelper.
It is working with the following code

record Row(string anbieter, string produkt, string? ean, string projektnr, string produktkennzeichen, string produktkategorie, string produktlink);

var config = new CsvConfiguration(CultureInfo.GetCultureInfo("de-DE"))
{
    HasHeaderRecord = true
};
using var file = File.OpenText("file.csv");
using var reader = new CsvReader(file, config);
var rows = reader.GetRecords<Row>().ToList();

However when I change the culture to en-US or InvariantCulture, I'm getting a HeaderValidationException.

Header with name 'anbieter'[0] was not found.
Header with name 'produkt'[0] was not found.
Header with name 'ean'[0] was not found.
Header with name 'projektnr'[0] was not found.
Header with name 'produktkennzeichen'[0] was not found.
Header with name 'produktkategorie'[0] was not found.
Header with name 'produktlink'[0] was not found.
Header with name 'anbieter'[0] was not found.
Header with name 'produkt'[0] was not found.
Header with name 'ean'[0] was not found.
Header with name 'projektnr'[0] was not found.
Header with name 'produktkennzeichen'[0] was not found.
Header with name 'produktkategorie'[0] was not found.
Header with name 'produktlink'[0] was not found.
Headers: 'anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink'
If you are expecting some headers to be missing and want to ignore this validation, set the configuration HeaderValidated to null. You can also change the functionality to do something else, like logging the issue.

IReader state:
   ColumnCount: 0
   CurrentIndex: -1
   HeaderRecord:
["anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink"]
IParser state:
   ByteCount: 0
   CharCount: 80
   Row: 1
   RawRow: 1
   Count: 1
   RawRecord:
anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink

Why does that happen? The header is not using any special characters like ä, ö, ü, ß, but only ASCII characters.

英文:

I'm trying to read a CSV file using CSVHelper.
It is working with the following code

record Row(string anbieter, string produkt, string? ean, string projektnr, string produktkennzeichen, string produktkategorie, string produktlink);


	var config = new CsvConfiguration(CultureInfo.GetCultureInfo(&quot;de-DE&quot;))
	{
		HasHeaderRecord = true
	};
	using var file = File.OpenText(&quot;file.csv&quot;);
	using var reader = new CsvReader(file, config);
	var rows = reader.GetRecords&lt;Row&gt;().ToList();

However when I change the culture to en-US or InvariantCulture, I'm getting a HeaderValidationException.

Header with name &#39;anbieter&#39;[0] was not found.
Header with name &#39;produkt&#39;[0] was not found.
Header with name &#39;ean&#39;[0] was not found.
Header with name &#39;projektnr&#39;[0] was not found.
Header with name &#39;produktkennzeichen&#39;[0] was not found.
Header with name &#39;produktkategorie&#39;[0] was not found.
Header with name &#39;produktlink&#39;[0] was not found.
Header with name &#39;anbieter&#39;[0] was not found.
Header with name &#39;produkt&#39;[0] was not found.
Header with name &#39;ean&#39;[0] was not found.
Header with name &#39;projektnr&#39;[0] was not found.
Header with name &#39;produktkennzeichen&#39;[0] was not found.
Header with name &#39;produktkategorie&#39;[0] was not found.
Header with name &#39;produktlink&#39;[0] was not found.
Headers: &#39;anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink&#39;
If you are expecting some headers to be missing and want to ignore this validation, set the configuration HeaderValidated to null. You can also change the functionality to do something else, like logging the issue.

IReader state:
   ColumnCount: 0
   CurrentIndex: -1
   HeaderRecord:
[&quot;anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink&quot;]
IParser state:
   ByteCount: 0
   CharCount: 80
   Row: 1
   RawRow: 1
   Count: 1
   RawRecord:
anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink

Why does that happen? The header is not using any special characters like ä, ö, ü, ß, but only ASCII characters.

答案1

得分: 1

CSVHelper 似乎在更改文化时使用了不同的分隔符,根据评论中指出的情况。
我在我的 CsvConfiguration 中添加了 DetectDelimiter = true,现在它可以在两种文化下工作了。

英文:

As pointed out in the comments, CSVHelper seems to use a different delimiter when changing the culture.
I added DetectDelimiter = true to my CsvConfiguration and it is now working for both cultures.

huangapple
  • 本文由 发表于 2023年2月24日 17:28:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定