如何通过读取C#中的另一列从csv文件中提取列? [英] How do I extract columns from csv file with reading another column in C#?
问题描述
我有这样的csv文件。
2; PlatesPatches; 2451; 1166.95410292072
2; PlatesPatches; 1389.5966620306; 788.205841446453
2; PlatesPatches; 1320.50069541029; 767.732962447844
2; PlatesPatches; 1356.32823365786; 754.937413073714
2; PlatesPatches; 1625.0347705146; 839.388038942976
2; PlatesPatches; 2205.95271210014; 1000.61196105702
2; PlatesPatches; 2451; 1056.9123783032
3; SmallPatchesmosaicpavement; 2354.38108484006; 977.579972183588
3; SmallPatchesmosaicpavement ; 2451; 1000.61196105702
3; SmallPatchesmosaicpavement; 2451; 1064.58970792768
3; SmallPatchesmosaicpavement; 1988.42837273992; 946.870653685675
3; SmallPatchesmosaicpavement; 1394.71488178025; 780.528511821975
3; SmallPatchesmosaicpavement; 1404.95132127956; 770.29207232267
3; SmallPatchesmosaicpavement; 2065.2016689847; 913.602225312935
4; Gravel; 2165.00695410292; 1062.03059805285
4; Gravel; 2141.97496522949; 1064.58970792768
4; Gravel; 2121.50208623088; 1067.1488178025
4; Gravel; 2106.14742698192; 1067.1488178025
4; Gravel; 2090.79276773296 ; 1067.1488178025
4; Gravel; 2075.43810848401; 1069.70792767733
4; Gravel; 2060.08344923505; 1072.26703755216
4; Gravel; 2039.61057023644; 1074.82614742698 >
4; Gravel; 2016.578581363; 1079.94436717663
4; Gravel; 1988.42837273992; 1082.50347705146
4; Gravel; 1955.15994436718; 1085.06258692629
4; Gravel; 1824.64534075104; 1015.96662030598
4; Gravel; 1916.77329624478; 1003.17107093185
4; Gravel; 1983.31015299026; 1000.61196105702
$关于第一列的b $ b我想打印第3和第4列的值。
当第一列值为2时,那么第3和第4列的所有值都会打印出来吗?
我尝试过:
OpenFileDialog opn = new OpenFileDialog();
if(opn.ShowDialog()== DialogResult.OK)
{
StreamReader sr = new StreamReader( opn.FileName);
List< string []> data = new List< string []>();
int Row = 0;
while(!sr.EndOfStream)
{
string [] Line = sr.ReadLine()。Split(',');
data.Add(Line);
行++;
Console.WriteLine(行);
}
}
I have a csv file like this.
2;PlatesPatches;2451;1166.95410292072
2;PlatesPatches;1389.5966620306;788.205841446453
2;PlatesPatches;1320.50069541029;767.732962447844
2;PlatesPatches;1356.32823365786;754.937413073714
2;PlatesPatches;1625.0347705146;839.388038942976
2;PlatesPatches;2205.95271210014;1000.61196105702
2;PlatesPatches;2451;1056.9123783032
3;SmallPatchesmosaicpavement;2354.38108484006;977.579972183588
3;SmallPatchesmosaicpavement;2451;1000.61196105702
3;SmallPatchesmosaicpavement;2451;1064.58970792768
3;SmallPatchesmosaicpavement;1988.42837273992;946.870653685675
3;SmallPatchesmosaicpavement;1394.71488178025;780.528511821975
3;SmallPatchesmosaicpavement;1404.95132127956;770.29207232267
3;SmallPatchesmosaicpavement;2065.2016689847;913.602225312935
4;Gravel;2165.00695410292;1062.03059805285
4;Gravel;2141.97496522949;1064.58970792768
4;Gravel;2121.50208623088;1067.1488178025
4;Gravel;2106.14742698192;1067.1488178025
4;Gravel;2090.79276773296;1067.1488178025
4;Gravel;2075.43810848401;1069.70792767733
4;Gravel;2060.08344923505;1072.26703755216
4;Gravel;2039.61057023644;1074.82614742698
4;Gravel;2016.578581363;1079.94436717663
4;Gravel;1988.42837273992;1082.50347705146
4;Gravel;1955.15994436718;1085.06258692629
4;Gravel;1824.64534075104;1015.96662030598
4;Gravel;1916.77329624478;1003.17107093185
4;Gravel;1983.31015299026;1000.61196105702
with respect to first column i want to print values of 3rd and 4th column.
like when first column value is 2 until then all the values of 3rd and 4th will print?
What I have tried:
OpenFileDialog opn = new OpenFileDialog();
if (opn.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(opn.FileName);
List<string[]> data = new List<string[]>();
int Row = 0;
while (!sr.EndOfStream)
{
string[] Line = sr.ReadLine().Split(',');
data.Add(Line);
Row++;
Console.WriteLine(Row);
}
}
推荐答案
这是关于同一主题的第三个问题,您似乎仍然不了解数据的格式。你的字符串[] Line = sr.ReadLine()。分割(',');
不起作用,因为你的数据使用了分号(';'
)字符作为字段分隔符,而不是逗号。我建议你学习使用OleDb导入文本文件(选项卡,CSV,自定义) [ ^ ]这将使事情变得更加简单。
This is your third question on the same subject and you still do not seem to understand the format of your data. Yourstring[] Line = sr.ReadLine().Split(',');
is not going to work because your data uses the semi-colon (';'
) character as a field delimiter, not the comma. I would suggest you study Using OleDb to Import Text Files (tab, CSV, custom)[^] which will make things much simpler.
这篇关于如何通过读取C#中的另一列从csv文件中提取列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!