使用c#将.XYZ转换为.csv

时间:2017-03-08 16:42:43

标签: c# csv

您好我正在使用此方法将“”替换为“,”但是当我尝试在具有32百万行的数据上使用它时失败。有谁知道如何修改它以使其运行?

            List<String> lines = new List<String>();

            //loop through each line of file and replace " " sight to ","
            using (StreamReader sr = new StreamReader(inputfile))
            {
                int id = 1;

                int i = File.ReadAllLines(inputfile).Count();
                while (sr.Peek() >= 0)
                {
                    //Out of memory issuee
                    string fileLine = sr.ReadLine();

                    //do something with line
                    string ttt = fileLine.Replace(" ", ", ");

                    //Debug.WriteLine(ttt);
                    lines.Add(ttt);
                    //lines.Add(id++, 'ID');
                }

                using (StreamWriter writer = new StreamWriter(outputfile, false))
                {
                    foreach (String line in lines)
                    {
                        writer.WriteLine(line+","+id);
                        id++;                     
                    }                       
                }                
            }

            //change extension to .csv
            FileInfo f = new FileInfo(outputfile);
            f.MoveTo(Path.ChangeExtension(outputfile, ".csv"));   

我一般我试图将.XYZ文件转换为.csv格式并在最后添加增量字段。我老生第一次使用c#说实话:)你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

请参阅上面的评论 - 您可以按如下方式修改您的阅读/写作:

  using (StreamReader sr = new StreamReader(inputfile))
  {
    using (StreamWriter writer = new StreamWriter(outputfile, false))
    {
      int id = 1;

      while (sr.Peek() >= 0)
      {
        string fileLine = sr.ReadLine();

        //do something with line
        string ttt = fileLine.Replace(" ", ", ");
        writer.WriteLine(ttt + "," + id);
        id++;
      }
    }
  }