如何读取CSV文件中的行,求和多少行 [英] How do I read lines in CSV file, sum a number of rows

查看:121
本文介绍了如何读取CSV文件中的行,求和多少行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用c#,streamreader和writer格式化了此数据,并创建了这个csv文件.以下是示例数据:

A ------ BC ----- D ------------ E ------------ F ------- G-- ----- H --------- I
新C A123 08/24/2011 08/24/2011 100.00 100.00 X123456 276135
新C A125 08/24/2011 08/24/2011 200.00 100.00 X123456 276135
新C A127 08/24/2011 08/24/2011 50.00 100.00 X123456 276135
新T A122 08/24/2011 08/24/2011 5.00 100.00 X225511 276136
新T A124 08/24/2011 08/24/2011 10.00 100.00 X225511 276136
新T A133 08/24/2011 08/24/2011 500.00 100.00 X444556 276137

我想要以下输出:

新C A123 08/24/2011 08/24/2011 100.00 100.00 X123456 276135
新C A125 08/24/2011 08/24/2011 200.00 100.00 X123456 276135
新C A127 08/24/2011 08/24/2011 50.00 100.00 X123456 276135
新C A001 08/24/2011 08/24/2011 350.00 100.00 X123456 276135
新T A122 08/24/2011 08/24/2011 5.00 100.00 X225511 276136
新T A124 08/24/2011 08/24/2011 10.00 100.00 X225511 276136
新T A001 08/24/2011 08/24/2011 15.00 100.00 X225511 276136
新T A133 08/24/2011 08/24/2011 500.00 100.00 X444556 276137
新T A001 08/24/2011 08/24/2011 500.00 100.00 X225511 276137

对于字段"I"的每次更改,我想添加一行,对F列求和,在C中添加"A001",然后复制内容
其他字段添加到该新添加的行中.列上的字母仅用于说明目的.没有标题.

首先,我应该先做什么?如何对F列求和,复制所有字段的内容,并向C添加"A001"?如何添加一行并复制带有I中每个更改的字段?

预先感谢您的帮助.

I formated 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?

Thank you in advance for your help.

推荐答案

您的逗号是可视化的:-).我今天感觉很慷慨.请执行以下操作:

You commas are visualized :-). I feel pretty generous today. Do the following:


  1. 创建代表一行(记录)的类或结构;我们称它为Record.
  2. 使用这些记录的列表.从字面上看,添加:

  1. Create a class or structure representing one line (record); let''s call it Record.
  2. Use the list of those records. Literally, add:
using RecordList = System.Collections.Generic.List<Record>;

  • 要读取文件,请使用System.IO.StreamReader;在"using"语句下创建它:

  • For reading a file, use System.IO.StreamReader; create it under "using" statement:

    RecordList list = new RecordList();
    using (System.IO.StreamReader reader = new System.IO.StreamReader(fileName) {
        //read lines here:
        string line = reader.ReadLine();
        //parse and validate each line
        Record record = //create instance of Record from parsed line
        list.Add(record);
    } //at this point, reader.Dispose will be called automatically, even if an exception was thrown

  • 写一种从记录中读取一行,对其进行解析和验证的方法. (上面显示为注释.)使用string.Split:
    开始解析

  • Write a method of reading one line from a record, parsing and validating it. (It''s shown as a comment above.) Start parsing using string.Split:

    //...
    string[] values = line.Split(',', System.StringSplitOptions.RemoveEmptyEntries);
    //convert strings into field of the type Record
    //use DateTime.Parse, double.Parse, uint.Parse, etc. See appropriate MSDN help pages
    //validate values as you parse
    //for example:
    record.F = double.Parse(values[5]); //may throw exception; decide how to handle it

  • 要查找总和,请遍历列表,例如:

  • For finding sums, iterate through the list, for example:

    double sumF = 0d;
    /...
    foreach (Record record in list) {
        sum.F += record.F;
        //...
    }

  • 如果您可能需要将数组用于其他目的,则上一步可能会显示为:

  • In case you might need an array for some other purposes, the previous step may look alternatively:

    Record[] records = list.ToArray();
    double sumF = 0d;
    /...
    foreach (Record record in records) {
        sum.F += record.F;
        //...
    }

  • 利润!


  • —SA



    —SA


    这篇关于如何读取CSV文件中的行,求和多少行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆