0

I have a lot of different CSV files with data in it (including headers).

I can't figure it out how to add a new column in the position number two.

I can fill the new cell with the null value (each row) but the new column is in first position.

Please see attachment image : the new column it must be after the column IDINTA.

enter image description here

Can anybody help me?

Thanks in advance

My code below.

output = System.Web.HttpContext.Current.Server.MapPath("/public/target.csv");

string[] CSVDump = File.ReadAllLines(output);
List<List<string>> CSV = CSVDump.Select(x => x.Split('|').ToList()).ToList();
for (int i = 0; i < CSV.Count; i++)
{
    CSV[i].Insert(0, i == 0 ? "Headername" : "Filename");
}
File.WriteAllLines(output, CSV.Select(x => string.Join("|", x)));

#Edit 01

enter image description here

#Edit 02

N|IDINTA|Filename|DDMMYYYY HHMMSS|
7
  • The way I can think of is; to convert csv to DataTable. Once data is there in DataTable, you can add column at whatever position you want. Commented Feb 17, 2017 at 13:48
  • So which of the columns is the one you want to insert? Can you show us what it should look like before and after? Commented Feb 17, 2017 at 13:59
  • @wkl please see #Edit 02 in my first question Commented Feb 17, 2017 at 14:05
  • Can you please explain which of csv tables is before, which is after and what the third one is? Commented Feb 17, 2017 at 14:07
  • @wkl : I need add new column Filename in csv file between column IDINTA and DDMMYYYY HHMMSS Commented Feb 17, 2017 at 14:08

2 Answers 2

2

Simply change the index in your Insert statement:

CSV[i].Insert(2, i == 0 ? "Headername" : "Filename");

Using a variable for the desired position, the complete code would be:

// define the desired position here:
int posNewColumn = 2;

output = System.Web.HttpContext.Current.Server.MapPath("/public/target.csv");

string[] CSVDump = File.ReadAllLines(output);
List<List<string>> CSV = CSVDump.Select(x => x.Split('|').ToList()).ToList();
for (int i = 0; i < CSV.Count; i++)
{
    if( CSV[i].Count > posNewColumn)
    {
        CSV[i].Insert(posNewColumn , i == 0 ? "Headername" : "Filename");
    }
    else 
    {
    // append the new data at the end if the existing line is too short.
    // You may want to do nothing instead or fill the appropriate number
    // of empty cells before adding.
        CSV[i].Add(i == 0 ? "Headername" : "Filename");
    }
}
File.WriteAllLines(output, CSV.Select(x => string.Join("|", x)));
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for help. please see #Edit 01 in my first question
@AntonioMailtraq If you get an error that the index must be within the limits of the list, I assume that there is a line in your csv which is shorter than the others. You should check for that. If this is a requirement, add it to the question.
it's possible that there is a line in your csv which is shorter than the others, but are hundreds of rows
edited. When the line is not long enough, we append it at the end of the line.
0

This should do it.

output = System.Web.HttpContext.Current.Server.MapPath("/public/target.csv");

string[] CSVDump = File.ReadAllLines(output);

var CSV = CSVDump.Select(x => x.Split('|').ToList());

var newInserted = CSV.Aggregate(new List<List<String>>(), (l, i) =>
{
    i.Insert(1, (l.Count == 0) ? "header" : "fileName");
    l.Add(i);
    return l;
});

File.WriteAllLines(output, newInserted.Select(x => string.Join("|", x)));

9 Comments

Thanks, but with your code the new column is not added.
lemme check, then, it's not appearing in the newInserted variable?
oops should be fixed now.
Thanks, the new column is not added
are you looking the CSV variable for the new column?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.