将.CSV文件读入控制台(问题) [英] Reading .CSV File Into Console (Problem)
问题描述
所以,我有一个问题,尝试将csv文件读入控制台,我目前有下面的代码,但有一个错误,我不明白,并搜索网络,无法找到。基本上它没有显示文件的内容,而是在控制台上显示此错误。
System.Collections.Generic.List'1 [System.String]
请有人帮我解决这个问题。
So, i have a problem with trying to read a csv file into a console, i currently have the below code which works but with one error i do not understand and have searched the web for and cant find. Basically instead of it showing the contents of the file it shows this error on the console.
"System.Collections.Generic.List'1[System.String]"
Cant anybody please help me solve this please.
using System;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var reader = new StreamReader(File.OpenRead(@"D:\Book1.csv"));
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
listA.Add(values[0]);
Console.WriteLine(listA);
}
}
}
}
推荐答案
它正在做你告诉它的事情要做的事 - 打印listA
的字符串表示。 :)
而是将Console.WriteLine
调用移至while循环外部并将其更改为:
It's doing what you told it to do - print the string representation oflistA
. :)
Instead move theConsole.WriteLine
call to outside your while loop and change it to:
foreach (string s in listA) {
Console.WriteLine (s);
}
/ ravi
没有一个Collection类实现ToString:这意味着使用默认的对象
版本 - 只打印类的名称,而不是内容。
所以
None of the Collection classes implement ToString: which means the use the defaultobject
version - which just prints the name of the class, not the content.
So
Console.WriteLine(listA);
隐式ToString方法是否在List上调用,并打印类的名称:它是一个通用的字符串列表。
如果你想打印列表的内容,那么你需要循环播放内容并依次打印每个项目:
Does an implicit ToString method call on the List, and prints the name of the class: "it's a generic List of strings".
If you want to print the content of the list, then you need to loop though teh content and print each item in turn:
foreach(string s in listA)
{
Console.WriteLine(s);
}
是一种方法。
这篇关于将.CSV文件读入控制台(问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!