从 CSV 中读取数据行然后显示数据

Read lines of data from CSV then display data

我必须从 txt 文件中读取信息,以某种方式(数组或列表)存储它,然后显示数据。计划必须包括至少一项额外的 class。 我碰壁了,无法进步。

字符串,字符串,双精度,字符串 姓名、徽章、薪资、职位 姓名、徽章、薪资、职位 姓名、徽章、薪水、职位

对不起,我知道下面的代码是灾难性的,但我不知所措,运行没时间了。

namespace Employees
{
    class Program
    {
        static void Main()
        {
            IndividualInfo collect = new IndividualInfo();

            greeting();
            collect.ReadInfo();
            next();
            for (int i = 0; i < 5; i++)
            {
                displayInfo(i);
            }
            exit();

            void greeting()
            {
                Console.WriteLine("\nWelcome to the Software Development Company\n");
            }
            void next()
            {
                Console.WriteLine("\n*Press enter key to display information . . . *");
                Console.Read();
            }
            void displayInfo(int i)
            {
                Console.WriteLine($"\nSoftware Developer {i + 1} Information:");
                Console.WriteLine($"\nName:\t\t\t{collect.nameList[i]}");
            }
            void exit()
            {
                Console.WriteLine("\n\n*Press enter key to exit . . . *");
                Console.Read();
                Console.Read();
            }
        }
    }
}

class IndividualInfo
    {
        public string Name { get; set; }
        //public string Badge{ get; set; }
        //public string Position{ get; set; }
        //public string Salary{ get; set; }

        public void ReadInfo()
        {
            int i = 0;
            string inputLine;
            string[] eachLine = new string[4];
            string[,] info = new string[5, 4]; // 5 developers, 4x info each
            StreamReader file = new StreamReader("data.txt");
            while ((inputLine = file.ReadLine()) != null)
            {
                eachLine = inputLine.Split(',');
                for (int x = 0; x < 5; x++)
                {
                    eachLine[x] = info[i, x];
                    x++;
                }
                i++;
            }
            string name = info[i, 0];
            string badge = info[i, 1];
            string position = info[i, 2];
            double salary = Double.Parse(info[i, 3]);
        }

        public List<string> nameList = new List<string>();
    }

到目前为止,我认为我可以用二维数组收集它,但列表会更好。另外,我在那里发布的代码不会 运行 因为我还没有找到显示它的方法。这就是我来这里的原因。

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]);
        }
    }
}

https://www.rfc-editor.org/rfc/rfc4180

using Microsoft.VisualBasic.FileIO;

var path = @"C:\Person.csv"; // Habeeb, "Dubai Media City, Dubai"
using (TextFieldParser csvParser = new TextFieldParser(path))
{
 csvParser.CommentTokens = new string[] { "#" };
 csvParser.SetDelimiters(new string[] { "," });
 csvParser.HasFieldsEnclosedInQuotes = true;

 // Skip the row with the column names
 csvParser.ReadLine();

 while (!csvParser.EndOfData)
 {
  // Read current line fields, pointer moves to the next line.
  string[] fields = csvParser.ReadFields();
  string Name = fields[0];
  string Address = fields[1];
 }
}

http://codeskaters.blogspot.ae/2015/11/c-easiest-csv-parser-built-in-net.html

LINQ 方式:

var lines = File.ReadAllLines("test.txt").Select(a => a.Split(';'));
var csv = from line in lines
          select (from piece in line
                  select piece);

^^错误 - Nick 编辑

原来的回答者似乎试图用二维数组填充 csv - 一个包含数组的数组。第一个数组中的每一项都包含一个表示该行号的数组,嵌套数组中的每一项都包含该特定列的数据。

var csv = from line in lines
          select (line.Split(',')).ToArray();

此问题已在此处得到完整解答:

Reading CSV file and storing values into an array