如何读取CSV文件中的行,求和多少行 [英] How do I read lines in CSV file, sum a number of rows
问题描述
我使用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:
- 创建代表一行(记录)的类或结构;我们称它为
Record
. - 使用这些记录的列表.从字面上看,添加:
- Create a class or structure representing one line (record); let''s call it
Record
. - Use the list of those records. Literally, add:
using RecordList = System.Collections.Generic.List<Record>;
System.IO.StreamReader
;在"using"语句下创建它: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
:开始解析
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
double sumF = 0d;
/...
foreach (Record record in list) {
sum.F += record.F;
//...
}
Record[] records = list.ToArray();
double sumF = 0d;
/...
foreach (Record record in records) {
sum.F += record.F;
//...
}
这篇关于如何读取CSV文件中的行,求和多少行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!