如何将.csv文件的每一行分隔成一个字符串列表> [英] How do I separate each line of a .csv file into a string list>

查看:679
本文介绍了如何将.csv文件的每一行分隔成一个字符串列表>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c#的新手,试图读取一个.csv文件,并把每一行文本放到一个单独的列表项中,以便以后排序。




1;final60;United Kingdom;2013-12-06 15:48:16;

2;donnyr8;荷兰;2013-12-06 15:54:32;

这是我第一次尝试不起作用。它显示Visual Studio 2010中没有错误,但是当我运行控制台程序,它会显示下列异常而不是列表。
类型'System.OutOFMemoryException'的异常被抛出。这是奇怪的,因为.csv文件只包含一个小列表。

  try 
{
//载入csv文件
使用(StreamReader file = new StreamReader (file.csv))
{
string line = file.ReadLine();
列表< string> fileList = new List< string>();
//对文件中的行进行一些操作,直到
结束//文件到达。
while(line!= null)
{

fileList.Add(line);

$ b foreach(fileList中的字符串fileListLine)
{
Console.WriteLine(fileListLine);


$ b catch(Exception e)
{
//让用户知道哪里出了问题。
Console.WriteLine(The file could not be read:);
Console.WriteLine(e.Message);

$ / code>

所以我正在接近这个正确的方式吗?$ b $如果您正在加载的文件不是很大,那么您可以使用
pre $ lt; code> List< ;串GT; list = File.ReadAllLines(file.csv)。ToList();

Servy在评论中指出,使用File.ReadLines 方法。

文件。ReadLines - MSDN


ReadLines和ReadAllLines方法的区别如下:当您使用
ReadLines时,您可以开始枚举
之前的字符串集合,返回整个集合;当使用ReadAllLines时,必须
等待返回字符串的整个数组,然后才能访问数组
。因此,当您处理非常大的文件时,
ReadLines可以更高效。

如果您需要 list< string> 然后你可以这样做:

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


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.

the .csv file is organised like so:

1;"final60";"United Kingdom";"2013-12-06 15:48:16";
2;"donnyr8";"Netherlands";"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. Exception of type 'System.OutOFMemoryException' was thrown. Which is bizarre because the .csv file only contains a small list.

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:

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

As Servy pointed out in comment it would be better to use File.ReadLines method.

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; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.

If you need a List<string> then you can do:

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

这篇关于如何将.csv文件的每一行分隔成一个字符串列表&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆