读取 CSV 文件并将值存储到数组中 [英] Reading CSV file and storing values into an array

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

问题描述

我正在尝试读取 *.csv 文件.

I am trying to read a *.csv-file.

*.csv 文件由以分号 (";") 分隔的两列组成.

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

我能够使用 StreamReader 读取 *.csv 文件,并能够使用 Split() 函数分隔每一行.我想将每一列存储到一个单独的数组中,然后显示它.

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.

能做到吗?

推荐答案

你可以这样做:

using System.IO;

static void Main(string[] args)
{
    using(var reader = new StreamReader(@"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天全站免登陆