阅读CSV文件和存储的值到一个数组 [英] Reading CSV file and storing values into an array

查看:326
本文介绍了阅读CSV文件和存储的值到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读 *。CSV -file。

* CSV - 文件包含由分号分隔两列( )。

The *.csv-file consist of two columns separated by semicolon (";").

我能够读 *。CSV -file使用的StreamReader,并能够将每个行使用斯普利特()功能。我想每一列存储到一个单独的阵列,然后显示它。

I am able to read the *.csv-file using StreamReader and able to separate each line by using the Split() function. I want to store each column into a separate array and then display it.

是否有可能做到这一点?

Is it possible to do that?

推荐答案

您可以做到这一点是这样的:

You can do it like this:

using System.IO;

static void Main(string[] args)
{
    var reader = new StreamReader(File.OpenRead(@"C:\test.csv"));
    List<string> listA = new List<string>();
    List<string> listB = new List<string>();
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        var values = line.Split(';');

        listA.Add(values[0]);
        listB.Add(values[1]);
    }
}

这篇关于阅读CSV文件和存储的值到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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