从URI更改编码CSV
我正在尝试下载一个CSV文件并解析其字段,并且正在使用CSVHelper库( https:httpps:// joshclose。 github.io/csvhelper )。 我认为面对的问题与编码有关,就像我在代码中阅读/打印文件内容时,CSVHelper库找不到相关的标题/字段。我在Word中导入该文件,UTF8似乎是正确的编码,以正确显示CSV内容。但是,我的代码和Excel无法正确显示标头(或任何捷克字符)。这是我获得Excel的方式:
var teamResultCsvUrl = new Uri("https://blabla/csv/4072");
这是我想做自己的事情的地方:
// total results for teams
using (response = await client.GetAsync(teamResultCsvUrl))
{
using (Stream stream = await response.Content.ReadAsStreamAsync())
{
using (StreamReader streamReader = new StreamReader(stream))
using (CsvHelper.CsvReader csvReader = new CsvHelper.CsvReader(streamReader, CultureInfo.InvariantCulture))
{
teamCsvResults = csvReader.GetRecords<ValidationClass>().ToList();
}
}
}
我会得到以下例外:
System.AggregateException: One or more errors occurred.
CsvHelper.HeaderValidationException: Header with name '<SOME HEADER>' was not found. 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.
at CsvHelper.Configuration.ConfigurationFunctions.HeaderValidated(Boolean isValid, String[] headerNames, Int32 headerNameIndex, ReadingContext context)
at CsvHelper.CsvReader.ValidateHeader(ClassMap map)
at CsvHelper.CsvReader.ValidateHeader(Type type)
at CsvHelper.CsvReader.ValidateHeader[T]()
at CsvHelper.CsvReader.<GetRecords>d__63`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
.......
当我打印文件内容时,字符未正确编码。如何/在哪里可以为下载的文件设置编码?我的方法中还有什么看起来不错的吗?
谢谢你!
I am trying to download a CSV file and parse its fields and I am using the CSVHelper library (https://joshclose.github.io/CsvHelper).
The issue which i face I think has to do with encoding, as when I read/print the file contents in my code the CsvHelper library can not find the relevant headers/fields. I imported the file in word and UTF8 seems to be the correct encoding in order to display the CSV contents correctly. However, my code and excel do not display the headers correctly (or any Czech characters) correctly. Here is how i am getting the excel:
var teamResultCsvUrl = new Uri("https://blabla/csv/4072");
And here is where i want to do my stuff:
// total results for teams
using (response = await client.GetAsync(teamResultCsvUrl))
{
using (Stream stream = await response.Content.ReadAsStreamAsync())
{
using (StreamReader streamReader = new StreamReader(stream))
using (CsvHelper.CsvReader csvReader = new CsvHelper.CsvReader(streamReader, CultureInfo.InvariantCulture))
{
teamCsvResults = csvReader.GetRecords<ValidationClass>().ToList();
}
}
}
I am getting the following exception:
System.AggregateException: One or more errors occurred.
CsvHelper.HeaderValidationException: Header with name '<SOME HEADER>' was not found. 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.
at CsvHelper.Configuration.ConfigurationFunctions.HeaderValidated(Boolean isValid, String[] headerNames, Int32 headerNameIndex, ReadingContext context)
at CsvHelper.CsvReader.ValidateHeader(ClassMap map)
at CsvHelper.CsvReader.ValidateHeader(Type type)
at CsvHelper.CsvReader.ValidateHeader[T]()
at CsvHelper.CsvReader.<GetRecords>d__63`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
.......
Also when I print the file contents, the characters are not encoded correctly. How/where can I set the encoding for the downloaded file? Is there anything else that looks wrong in my approach?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CsvHelper
不控制用于读取数据的编码。您可以指定创建StreamReader
时要使用的编码。如果
Encoding.UTF8
不起作用,您可能需要找出正确的编码。CsvHelper
does not control the encoding used to read the data. You can indicate the encoding to use when creating theStreamReader
.If
Encoding.UTF8
doesn't work, you may need to figure out what the correct encoding is.