简体   繁体   English

如何将.csv文件的每一行分隔为字符串列表>

[英]How do I separate each line of a .csv file into a string list>

I am new to c# and am attempting to read in a .csv file and put each line of text in to a separate list item so I can sort it later. 我是c#的新手,我正在尝试读取.csv文件并将每行文本放入一个单独的列表项中,以便稍后对其进行排序。

the .csv file is organised like so: .csv文件的组织方式如下:

1;"final60";"United Kingdom";"2013-12-06 15:48:16"; 1;“final60”;“英国”;“2013-12-06 15:48:16”;
2;"donnyr8";"Netherlands";"2013-12-06 15:54:32"; 2;“donnyr8”;“荷兰”;“2013-12-06 15:54:32”; etc 等等

This is my first attempt that doesn't work.It shows no errors in Visual studios 2010 but when I run the console program it displays the following Exception instead of the list. 这是我的第一次尝试不起作用。它在Visual Studio 2010中没有显示错误,但是当我运行控制台程序时,它显示以下异常而不是列表。 Exception of type 'System.OutOFMemoryException' was thrown. Which is bizarre because the .csv file only contains a small list. 这很奇怪,因为.csv文件只包含一个小列表。

try
{
// load csv file
using (StreamReader file = new StreamReader("file.csv"))
      {
       string line = file.ReadLine();
       List<string> fileList = new List<string>();
       // Do something with the lines from the file until the end of  
       // the file is reached. 
       while (line != null)
       {

          fileList.Add(line);

        }
        foreach (string fileListLine in fileList)
         {
            Console.WriteLine(fileListLine);
         }
       }
}
catch (Exception e)
{
  // Let the user know what went wrong.
   Console.WriteLine("The file could not be read:");
   Console.WriteLine(e.Message);
}

So am I approaching this the correct way? 我正在以正确的方式接近这个吗?

If the file you are loading isn't really big then you can use File.ReadAllLines : 如果您加载的文件不是很大,那么您可以使用File.ReadAllLines

List<string> list = File.ReadAllLines("file.csv").ToList();

As Servy pointed out in comment it would be better to use File.ReadLines method. 正如Servy在评论中指出的那样,最好使用File.ReadLines方法。

File.ReadLines - MSDN File.ReadLines - MSDN

The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; ReadLines和ReadAllLines方法的不同之处如下:使用ReadLines时,可以在返回整个集合之前开始枚举字符串集合; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. 当您使用ReadAllLines时,必须等待返回整个字符串数组才能访问该数组。 Therefore, when you are working with very large files, ReadLines can be more efficient. 因此,当您使用非常大的文件时,ReadLines可以更高效。

If you need a List<string> then you can do: 如果您需要List<string>那么您可以:

List<string> list = File.ReadLines("file.csv").ToList();

You are not updating the line variable so the line will be always different from null infinite loop which cause OutOfMemoryException 您没有更新line变量,因此该行将始终与null无限循环不同,这会导致OutOfMemoryException

 try
    {
    // load csv file
    using (StreamReader file = new StreamReader("file.csv"))
          {
           string line = file.ReadLine();
           List<string> fileList = new List<string>();
           // Do something with the lines from the file until the end of  
           // the file is reached. 
           while (line != null)
           {

              fileList.Add(line);
               line = file.ReadLine();

            }
            foreach (string fileListLine in fileList)
             {
                Console.WriteLine(fileListLine);
             }
           }
    }

but the correct approaches will be 但正确的方法将是

List<string> list = File.ReadLines("file.csv").ToList();

which is better than File.ReadAllLines for the following reason From MSDN : 由于以下原因,它优于File.ReadAllLines MSDN

When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned;

You should use File.ReadAllLines() and then parse the strings in the array. 您应该使用File.ReadAllLines()然后解析数组中的字符串。 For extremely large files this might not be feasible and you'll have to stream the single lines in and process them one by one. 对于非常大的文件,这可能是不可行的,您必须逐行传输单行并逐个处理它们。 But this is something you can only decide AFTER you have seen this quick approach failing miserably. 但这是你只能在看到这种快速方法失败后才能决定的事情。 Until then, stick to the quick and dirty. 在那之前,坚持快速和肮脏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM