reader.TryGetField(string, out field) not getting first column #1216
Replies: 6 comments
-
Can you break this down into a small self contained example that fails? Something similar to this. void Main()
{
var s = new StringBuilder();
s.AppendLine("SettingCode,Value,Type");
s.AppendLine("joke,me,jerk");
s.AppendLine("fudge,me,fork");
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader))
{
while (csv.Read())
{
if (csv.Context.Row == 1)
{
csv.ReadHeader();
continue;
}
string field;
csv.TryGetField("SettingCode", out field);
field.Dump();
csv.TryGetField("Value", out field);
field.Dump();
csv.TryGetField("Type", out field);
field.Dump();
}
}
} Leave out all the extra stuff and narrow it down to the failure. |
Beta Was this translation helpful? Give feedback.
-
I will try to get to this, bogged down with work right now |
Beta Was this translation helpful? Give feedback.
-
I am having this issue as well. The .csv file in question is created by a Linux machine and therefore uses LF as the only new line character (as opposed to also using CR as with Windows). When parsing this file with LF characters in a Windows environment, the TryGetField function fails to retrieve any values in the first field for the entire file. If I add a garbage/filler column as the first column, everything works fine for my implementation. Also, if I create the file in Windows with the same information, everything works fine. For me, it's only when the LF character is the only new line character and the app is running in Windows that TryGetField fails. Edit: I believe it might be an encoding issue. Because if I encode the file in UTF-8, instead of UTF-8-BOM, the file is parsed fine regardless of which character is used for designating a new line. |
Beta Was this translation helpful? Give feedback.
-
@joelenelson Would it work to specifically tell it to detect the encoding from byte order marks? using (var reader = new StreamReader("path\\to\\file.csv", detectEncodingFromByteOrderMarks: true))
using (var csv = new CsvReader(reader))
{
var records = csv.GetRecords<Foo>();
} |
Beta Was this translation helpful? Give feedback.
-
That would work great. Except that my implementation uses StringReader instead of StreamReader. No such encoding overload exists for StringReader. |
Beta Was this translation helpful? Give feedback.
-
I changed my implementation to use a stream instead of a string, and to detect the encoding as suggested, and the encoding problem is gone. Thank you. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a very simple csv file that i would like to parse by column names (header record) using TryGetValue(string, out field).this works correctly for the second and third columns, but not the first.
It
appears that the NamedIndexCache does not have the first column, although Named indexes does. (although you can't get it by key).
this finds a value: reader.Context.NamedIndexes.Where(n=>n.Key.Equals(FileColumns.SettingCode, StringComparison.InvariantCultureIgnoreCase))
this does not find a value. reader.Context.NamedIndexes.Where(n=>n.Key == FileColumns.SettingCode)
I think there may be control characters being read in with the first column header.
using csvhelper 12.1.1, and a csv generated by excel 2017

below is the code, and attached are screenshots of my tests.
settingsimport.csv.txt
Beta Was this translation helpful? Give feedback.
All reactions