How to remove Multiple Headers from CSV file

Hi All,
I have a case were i have to remove the multiple headers present in the file at different levels of the whole file.
Eg
Customer,Address,PhoneNo
abc,ewq,1234
fdg,sdfsd,4234
Customer,Address,PhoneNo
fdsad,fsas,45345
fsdgf,fdhfdg,5656

So now i have to remove rest of the headers from the file present in the subsequent rows if any and keep only the inital first header which is at the start of the file.

we can readin all lines into an array and then filter out with LINQ all lower lines which are same as the first header line. Let us know if you need more help on this

can you share the code for it

You can simply use the for each loop and remove the headers,

Do like: row(1).ToString.contains(“Customer”) then remove the row.

You can use Remove Data row activity for the same.

@vishal_nachankar

dont want to loop through it as it has lot of rows

Used Filter Datatable and removed those rows thanks

grafik

arrLines = File.ReadAllLines(Path.Combine(Environment.CurrentDirectory,"__Tasks\CSV\RemoveRepeatingHeaderLines","Data.csv"))

arrFilteredLines = arrLines.Where(Function (x) Not x.Equals(arrLines(0))).Prepend(arrLines(0)).toArray

OR

arrFilteredLines = arrLines.Except({arrLines(0)}).Prepend(arrLines(0)).toArray

The result we can pass to generate Datatable with a String.Join(Environment.NewLine, arrFilteredLines) or can write out to a text file for later readin with read csv file.

ok let me try it

Need the code in C# as my framework is in C#.
I am getting error if trying to use the above code

no problem, just use the correspoding C# Syntax

not able to find the exact syntax for the linq can you please provide it

File.ReadAllLines("YourPath");
arrLines.Except(new String[] {arrLines.First()}).Prepend(arrLines.First()).ToArray();

this code is working but i am not able to copy this details to a text file and save it

write text file activity

Tried using that but it is string array and write text except a string … looping through the whole file will consume a lot of time if it has more lines

just make a joined string with the String.Join method
Also have a look on the File.WriteAllLine method like: File.WriteAllLines(Path,arrLinesFiltered);

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.