Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
comming from mostly a php background. how do I compare my csvs?

so wat i have is 2 csv files
1. a small file
2. big file
I want to check if the rows from small file are in the big file. if yes then it will rewrite those rows in a new script
if not then they wont be in the new script

code
C#
<pre>static void CompanyList()
        {
            //hij haalt de postcodes csv op 
            string path = @"CompanyList.csv";

            // Calling the ReadAllLines() function put information in array zipCodes
            string[] buisnesses = File.ReadAllLines(path);
            //hij haalt de postcodes csv op 
            string path2 = @"Postcodes.csv";

            // Calling the ReadAllLines() function put information in array zipCodes
            string[] zipCodes = File.ReadAllLines(path2);


            //List<zipCodeData> zipCodeList = new List<zipCodeData>();
            List<string> buisnesses2 = new List<string>(); // make a list named ZipCodes2

            foreach (string s in buisnesses) // for each string in zipCodes do{}
            {
                //foreach list
                string[] split = s.Split(';'); // check if file has ; and remove all those
                //checkt of bijde files gelijk zijn wat eigenlijk nodig is checken of de values van company list voor komen in postcodes.csv
                if (path == path2)
                {
                    //dus moet er natuurlijk doorheen lopen
                   MessageBox.Show("the two files are the same ");
                    break;
                }
                else
               {
                  MessageBox.Show("They are not the same");
                  break;

               }

                //string modified = split[0].Insert(4, " "); // add extra white spage in firt split (postcode)

                string newRow = split[0] + ";" + split[1] + ";" + split[2] + ";" + split[3] + ";" + split[4] + ";" + split[5] + ";" + split[6] + ";" + split[7];
                buisnesses2.Add(newRow); // add a new row to the list
            }

            File.WriteAllLines(@"Resultlist.csv", buisnesses2.ToArray()); // make a new file for the companys
        }


What I have tried:

tried looking it up but then you get check if 2 files are compleetly equal to each other and I dont want that
Posted
Updated 4-Apr-23 1:55am
Comments
PIEBALDconsult 4-Apr-23 8:36am    
Load them into a database and compare there.

There is no "standard way" to do that so you will have to create your own - and how you do that depends on how "completely equal" the data in the files is - CSV is a "flexible" format, and the same data written by two different apps could be different in the file.
1) There is no requirement to have a header line, but it is allowed.
2) There is no requirement to use double quotes around text, and they aren't needed unless the data contains commas (in which case the data string needs to be in double quotes, with double quote characters doubled:
1, The quick brown fox, 3
is the same data as
1, "The quick brown fox", 3
And
1, The quick "brown" fox, 3
is the same as
1, "The quick ""brown"" fox", 3
and so on.

So either you would need to process them all yourself as text files, checking for duplicates and following the rules quoting rules, or you would have to read the data into your app using your preferred reader, and then check for duplicates using probably a hashset or by comparign the data for each row in turn, using a loop or Linq.
 
Share this answer
 
Comments
Rebecca2002 4-Apr-23 7:55am    
thanks
OriginalGriff 4-Apr-23 8:25am    
You're welcome!

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