Open h-hasanov opened 7 years ago
Did you try putting false for 'hasHeaders' ? On Tue, Feb 14, 2017 at 5:37 PM vSkullv notifications@github.com wrote:
Hi,
I have a csv file with duplicated column names. When I try to read the csv the following exception is thrown "System.ArgumentException : An item with the same key has already been added." Any ideas how I can avoid this?
Thanks
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/phatcher/CsvReader/issues/39, or mute the thread https://github.com/notifications/unsubscribe-auth/AAxOx2ugbLvEO8CzkAsVF8YiwrSEZWP3ks5rciy_gaJpZM4MBFcm .
Setting hasHeaders to false would definitely help but then I have to manage the headers myself (which is trivial). I was hoping that there is a magic switch that I can use.
You might be able to write an extension. You will still have to do the management and hasHeasers=false but at least your main code will still look nice.
csv.GetValue("dup_col", 0);
csv.GetValue("dup_col", 1);
csv.GetValue("unique_col");
I ended up with something similar and it works now. Thanks for the help.
To make it more general, you could introduce a policy that adds a suffix e.g. dup_col, dup_col1.
The default behaviour could be to throw the duplicate column exception, and then this duplicate handling policy could be added it if needed.
Problem about doing this by default is generally people's CSV files are well-formed and you want to know if they are not.
Makes sense. Thanks for the clarification.
PS: I am truly amazed by the performance of the library.
Hello phatcher. Great library. Thank you for open sourcing it.
I am having the same issue with duplicate headers coming in. We have a mapping engine and cannot control the input data. Sometimes duplicate headers come in. In our current solution we modified the code to add the index to the header name if it is duplicated. Perhaps the user of the library could control this behavior with an event? Something like,
public EventHandler<DuplicateHeaderEventArgs> OnDuplicateHeader;
where the eventargs are something like.
public DuplicateHeaderEventArgs(string headerName, int index, int firstOccuranceIndex)
{
HeaderName = headerName;
Index = index;
FirstOccuranceIndex = firstOccuranceIndex;
}
// Name of the header
public string HeaderName { get; set; }
// Index of the duplicated Index
public int Index { get; }
// Index of the first occurance of the HeaderName
public int FirstOccuranceIndex { get; }
}
If OnDuplicateHeader is null, the current behavior of throwing an exception would occur. However; it would be nice to have the exception include which header column caused the exception. I think currently you get a generic exception that doesn't include this information.
Thoughts on adding OnDuplicateHeader event handler? I could work on creating a pull request, or do you prefer to see this done a different way like a IColumnHeaderNamePolicy?
@jonreis I think a IColumnHeaderNamePolicy is the way to go here, or may be a IColumnHeaderPolicy as that might allow us to generalise the default/override behaviour
@phatcher duplicate Column issue is resolved? If Yes then what's solution ?
Hi,
I have a csv file with duplicated column names. When I try to read the csv the following exception is thrown "System.ArgumentException : An item with the same key has already been added." Any ideas how I can avoid this?
Thanks