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
Support for Header Substring #1039
Comments
void Main()
{
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader))
{
writer.WriteLine("Id(1),Name(2)");
writer.WriteLine("1,one");
writer.WriteLine("2,two");
writer.Flush();
stream.Position = 0;
csv.Configuration.PrepareHeaderForMatch = header => header.Substring(0, header.IndexOf("("));
csv.GetRecords<dynamic>().ToList().Dump();
}
} |
Cool thanks. Is there any way to improve performance for this? I have csv file that contains close to 100K records. And on mobile device it takes 23 for this: |
Don't use |
Ok. But the problem is that I have to handle that in generic way, so I can not use normal class not calling Do you have on the road map performance improvements for this use case? |
You should be able to handle reading in a generic way easier using |
The thing is that because of the requirements, the whole model is completely generic, name of the fields are not known, can be anything, whatever user puts in the CSV file. That is the reason why I am using dynamic. Later on I access each property from dynamic object, which I also do not like very much, but currently I do not have other solution. |
Can you give an example of how you're getting the data out of the dynamic object? Maybe I can translate that into using CsvHelper directly to do it instead, to speed it up. |
Sure I can. Here is how it looks:
Please note that calling Get records takes approx. 23 seconds. |
If your'e doing Try doing it like this. It should be a lot faster. void Main()
{
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader))
{
writer.WriteLine("Id(1),Name(2)");
writer.WriteLine("1,one");
writer.WriteLine("2,two");
writer.Flush();
stream.Position = 0;
csv.Configuration.PrepareHeaderForMatch = header => header.Substring(0, header.IndexOf("("));
csv.Read();
csv.ReadHeader();
while (csv.Read())
{
for (var i = 0; i < csv.Context.HeaderRecord.Length; i++)
{
var field = csv.GetField(i);
}
}
}
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
}
public class TestMap : ClassMap<Test>
{
public TestMap()
{
Map(m => m.Id);
Map(m => m.Name);
}
} |
@JoshClose I'm probably missing something, but wouldn't the example code above, calling And with that said, in both cases you don't actually have the entire file into memory at any one point, as previous records get GC'ed... |
Sorry, you are correct. It The speed improvements come from not using |
Hi Josh. Thanks. I give it a try and get back to you with results. |
Hello,
Is there a way to configure CSVReader to take only portion of CSV header?
I am using generic way for parsing, like this:
In the CSV file, in the header I have something like this Name1(2,3) | Name2(4,2)...
So, instead taking Name1(1,3), I would like to take part of column name up to brackets Name1.
Is this possible?
Regards,
Novak
The text was updated successfully, but these errors were encountered: