Home > Net >  Updating several lines of a csv
Updating several lines of a csv

Time:03-18

I am trying to update a CSV file with data from a textbox and running into issues.

Example CSV:
test1,data1,moredata1
test2,data2,moredata2
test3,data3,moredata3
test4,data4,moredata4
test5,data5,moredata5
test6,data6,moredata6
test7,data7,moredata7
test8,data8,moredata8
test9,data9,moredata9
test10,data10,moredata10

Expected CSV:
test1,Change1,moredata1
test2,data2,moredata2
test3,data3,moredata3
test4,Change2,moredata4
test5,data5,moredata5
test6,data6,moredata6
test7,data7,moredata7
test8,data8,moredata8
test9,data9,moredata9
test10,Change3,moredata10

Actual CSV

test1,Change1
,moredata1
test2,data2,moredata2
test3,data3,moredata3
test4,data4,moredata4
test5,data5,moredata5
test6,data6,moredata6
test7,data7,moredata7
test8,data8,moredata8
test9,data9,moredata9
test10,data10,moredata10
test1,Change1
,moredata1
test2,data2,moredata2
test3,data3,moredata3
test4,Change2
,moredata4
test5,data5,moredata5
test6,data6,moredata6
test7,data7,moredata7
test8,data8,moredata8
test9,data9,moredata9
test10,data10,moredata10
test1,Change1
,moredata1
test2,data2,moredata2
test3,data3,moredata3
test4,data4,moredata4
test5,data5,moredata5
test6,data6,moredata6
test7,data7,moredata7
test8,data8,moredata8
test9,data9,moredata9
test10,Change3,moredata10
test1,Change1
,moredata1
test2,data2,moredata2
test3,data3,moredata3
test4,Change2
,moredata4
test5,data5,moredata5
test6,data6,moredata6
test7,data7,moredata7
test8,data8,moredata8
test9,data9,moredata9
test10,Change3,moredata10

Code:

var importboxData = massimportBox.Text.Split('\n');
                foreach (var iLine in importboxData)
                {
                    using (StreamReader reader = new StreamReader(path))
                    {
                        String line;

                        while ((line = reader.ReadLine()) != null)
                        {
                            if (line.Split(',')[0].Equals(iLine.Split(',')[0]))
                            {
                                String[] split = line.Split(',');
                                int indexNum = Int32.Parse(indexBox.Text);

                                split[indexNum] = iLine.Split(',')[1];
                                line = String.Join(",", split);
                            }
                            lines.Add(line);
                        }
                    }
                    using (StreamWriter writer = new StreamWriter(path, false))
                    {
                        foreach (String line in lines)
                            writer.WriteLine(line);
                    }
                }

Form:
enter image description here

Using updated code from Answer:
enter image description here

CodePudding user response:

You're unnecessarily reading and writing the whole file for every single desired change.

Try something more like:

Dictionary<String, String> changes = new Dictionary<string, string>();
foreach (String iLine in massimportBox.Lines)
{
    String[] values = iLine.Split(',');
    if (values.Length==2 && values[0].Trim().Length>0)
    {
        changes[values[0]] = values[1];
    }
}

int indexNum = Int32.Parse(indexBox.Text);

String[] lines = System.IO.File.ReadAllLines(path);
for (int i=0; i<lines.Length; i  )
{
    String[] values = lines[i].Split(',');
    String key = values[0];
    if (changes.ContainsKey(key) && indexNum>=0 && indexNum<values.Length)
    {
        values[indexNum] = changes[key];
        lines[i] = String.Join(",", values);
    }
}

System.IO.File.WriteAllLines(path, lines);

CodePudding user response:

Your problem is with nested loops. Your code will repeat the file reading once for each requested change. Try the following solution where a dictionary of <key, cahnge> values ​​is created and the file is read once.

var importboxData = massimportBox.Text.Split('\n');
Dictionary<string,string> iSplitDict = new Dictionary<string,string> ()
foreach (var iLine in importboxData)
{
    string[] iSplit = iLine.Split(',');
    iSplitDict.Add(iSplit[0], iSplit[1])
}

using (StreamReader reader = new StreamReader(path))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        string[] split = line.Split(',');
        if (iSplitDict.ContainsKey(split[0]))
        {
            int indexNum = Int32.Parse(indexBox.Text);
            split[indexNum] = iSplitDict[split[0]];
            line = String.Join(",", split);
        }
        lines.Add(line);
    }

    using (StreamWriter writer = new StreamWriter(path, false))
    {
        foreach (String line in lines)
        {
            writer.WriteLine(line);
        }
    }
}
  •  Tags:  
  • c#
  • Related