简体   繁体   中英

C# and the CSV file

I formatted this data using c#, streamreader and writer and created this csv file. Here is the sample data:

A------B-C---D----------E------------F------G------H-------I

NEW,  C,A123 ,08/24/2011,08/24/2011 ,100.00,100.00,X123456,276135

NEW,  C,A125 ,08/24/2011,08/24/2011 ,200.00,100.00,X123456,276135

NEW,  C,A127 ,08/24/2011,08/24/2011 , 50.00,100.00,X123456,276135

NEW,  T,A122 ,08/24/2011,08/24/2011 ,  5.00,100.00,X225511,276136

NEW,  T,A124 ,08/24/2011,08/24/2011 , 10.00,100.00,X225511,276136

NEW,  T,A133 ,08/24/2011,08/24/2011 ,500.00,100.00,X444556,276137

I would like the following output:

NEW,  C,A123 ,08/24/2011,08/24/2011 ,100.00,100.00,X123456,276135

NEW,  C,A125 ,08/24/2011,08/24/2011 ,200.00,100.00,X123456,276135

NEW,  C,A127 ,08/24/2011,08/24/2011 , 50.00,100.00,X123456,276135

NEW,  C,A001 ,08/24/2011,08/24/2011 ,350.00,100.00,X123456,276135

NEW,  T,A122 ,08/24/2011,08/24/2011 ,  5.00,100.00,X225511,276136

NEW,  T,A124 ,08/24/2011,08/24/2011 , 10.00,100.00,X225511,276136

NEW,  T,A001 ,08/24/2011,08/24/2011 , 15.00,100.00,X225511,276136

NEW,  T,A133 ,08/24/2011,08/24/2011 ,500.00,100.00,X444556,276137

NEW,  T,A001 ,08/24/2011,08/24/2011 ,500.00,100.00,X225511,276137

With each change in field "I", I would like to add a line, sum column F, add a "A001" to C, and copy the contents of the other fields into that newly ADDed line.

The letters on the columns are for illustrative purposes only. There are no headers.

First, what should I do first? How do I sum column F, copy contents of all fields, and add "A001" to C? How do I add a line and copy the fields w/ each change in I?

From your questions it doesn't sound like your data fits a flat file format very well, or at least not a CSV (analogy to a single DB table). Wanting to 'copy the contents of the other fields into that newly ADDed line' implies to me that there is possibily a relationship that might be better expressed referentially rather than copying the data to a new row.

Also keep in mind the requirement to sum according to column 'F' suggests that you will need to iterate over every row in order to calculate the sum.

If you decide to go a route other than a CSV your might try a light weight database solution such as SQLite . An alternative might be to look at XmlSerializer or DataContract (with Serializer) and just work with objects in your code. The objects can the be serialized to the disk when you're done with them.

You could use a custom iterator block, just as an example this is showing how to add the new line that contains your "A001" C column. You can just as easily add a new sum column - keep in mind though that you should keep the number of columns in each row the same.

public IEnumerable<string> GetUpdatedLines(string fileName)
{
    int? lastValue = null;
    foreach (string line in File.ReadLines(fileName))
    {
        var values = line.Split(',');
        int myIValue = Convert.ToInt32(values[7]);
        if (lastValue.HasValue && myIValue != lastValue)
        {
            //emit new line sum
            values[2] = "A001";
            yield return string.Join(",", values);
        }
        lastValue = myIValue;
        yield return line;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM