CsvReader icon indicating copy to clipboard operation
CsvReader copied to clipboard

Exception thrown with duplicated colum names

Open h-hasanov opened this issue 8 years ago • 10 comments

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

h-hasanov avatar Feb 14 '17 22:02 h-hasanov

Did you try putting false for 'hasHeaders' ? On Tue, Feb 14, 2017 at 5:37 PM vSkullv [email protected] 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 .

joshgo avatar Feb 14 '17 22:02 joshgo

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.

h-hasanov avatar Feb 15 '17 08:02 h-hasanov

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");

joshgo avatar Feb 16 '17 13:02 joshgo

I ended up with something similar and it works now. Thanks for the help.

h-hasanov avatar Feb 16 '17 14:02 h-hasanov

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.

phatcher avatar Feb 16 '17 17:02 phatcher

Makes sense. Thanks for the clarification.

PS: I am truly amazed by the performance of the library.

h-hasanov avatar Feb 16 '17 21:02 h-hasanov

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.

jonreis avatar Mar 03 '17 19:03 jonreis

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 avatar Mar 08 '17 16:03 jonreis

@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 avatar Jan 27 '18 16:01 phatcher

@phatcher duplicate Column issue is resolved? If Yes then what's solution ?

dkt2000 avatar Aug 31 '21 15:08 dkt2000