[英]Reading and display the csv file that is read in C#
I just started learning C#, so sorry if this if this is an elementary problem.我刚开始学习 C#,如果这是一个基本问题,那么抱歉。
I wrote a little code to display read in the csv files, but I am not sure how to display what is being read in. Below is the code I have written:我写了一些代码来显示 csv 文件中的读取内容,但我不确定如何显示正在读取的内容。以下是我编写的代码:
static void Main(string[] args)
{
var reader = new StreamReader(File.OpenRead(@"C:\Users\syang\Desktop\file.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]);
}
}
Any help will be greatly appreciated.任何帮助将不胜感激。 Thanks!谢谢!
Create a method for displaying创建显示方法
static void Display(string input)
{
Console.Write(input + " ");
}
Then in your Main method just call it for every member at the end...然后在你的 Main 方法中,最后为每个成员调用它......
...
listA.Add(values[0]);
listB.Add(values[1]);
}
listA.ForEach(Display);
If you want a different format for displaying items, you can change the Console.Write
part.如果您想要显示项目的不同格式,您可以更改Console.Write
部分。
If you want them on a single line or something else that's just as simple, you can do it in one line:如果您希望它们在一行上或其他同样简单的东西,您可以在一行中完成:
listA.ForEach(Console.WriteLine);
However, in this version of your code, you're making 2 assumptions:但是,在此版本的代码中,您做出了两个假设:
There's no definitive spec for the CSV format, so unless you're creating the CSV yourself (or you get it from a trusted entity), you can't trust anyone with those three above. CSV 格式没有明确的规范,因此除非您自己创建 CSV(或者您从受信任的实体获得它),否则您不能信任具有上述三种格式的任何人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.