Click here to Skip to main content
14,920,488 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 2 csv file i need to merge in one file and need to remove duplicate column in merged csv file

What I have tried:

var first = File.ReadAllLines("C:\\Task\\Input\\UserInfo.csv");
            var second = File.ReadAllLines("C:\\Task\\Input\\PersonalInfo.csv");
            var result = first.Zip(second, (f, s) => string.Join(",", f, s));
            File.WriteAllLines("C:\\Divyashree\\Dot_net\\Task\\Input\\combined.csv", result);
Posted
Updated 29-Apr-21 22:14pm

1 solution

In order to do that, you first have to read your file as a CSV - and find some way to relate "row X" of file 1 with "row Y" of file 2 - then identify the duplicate columns to build a new composite dataset which you can write to a new file.

Just reading lines won't do it: that's treating CSV data as if it was a "straight text file" which it is, but that text has a layer of formatting applied which you need to utilize in order to get this to work.

And ... a CSV data column can be spread across multiple lines, if inclosed in double quotes - so you current approach will not only not allow you to identify any columns, but could seriously mess up your data integrity if you are unlucky.

I'd start by reading each CSV file into a DataTable (A Fast CSV Reader[^] could help you there) then combining them into a single new DataTable to write out to a new file.
It's a lot less effort (and more maintainable) than trying to "roll your own" CSV data reader!
   
Comments
Member 15088142 30-Apr-21 4:57am
   
can i have some sample code to do this
OriginalGriff 30-Apr-21 5:29am
   
Which bit?
What part did you try, and where did you get stuck?
Member 15088142 30-Apr-21 5:34am
   
List<string> UserInfo = new List<string>() { "Sno", "Name" };
List<string> PrsnInfo = new List<string>() { "Sno", "Gender", "State" };
var first = File.ReadAllLines("C:\\Divyashree\\Dot_net\\Task\\Input\\UserInfo.csv");
var second = File.ReadAllLines("C:\\Divyashree\\Dot_net\\Task\\Input\\PersonalInfo.csv");
created 2 list and trying to compare if exit need to remove
OriginalGriff 30-Apr-21 5:48am
   
So you've ignored everything I said then?
Member 15088142 30-Apr-21 5:49am
   
no i dont understand that
OriginalGriff 30-Apr-21 6:20am
   
What part doesn't make sense?

I'm not trying to be funny, or awkward - we only get to see exactly what you type, so if you don't tell us something we don't know it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900