如何从CSV文件流中删除页眉?

问题描述 投票:0回答:1

我必须对csv文件的上传进行一定的限制。

  • 我将处理 "大 "CSV文件(包含头行)。
  • 我需要从CSV文件中删除第一行标题。
  • 文件上传代码需要一个 文件流(不包含文件头) 作为输入!(因为我受限于在这个流(包含无头的csv数据)上面做很多流操作 (因为我被限制在这个流(包含无头csv数据)上面做很多流操作)

包装器C#代码。

using (var stream = File.OpenRead("C:\~~~\~~~\~~~\SampleFile.csv"))
{
    //CSV Header removal snippet - which gives me a new stream containing data without headers.
    ~
    ~
    ~
    ~
    //All my stream handling code of chunking stream into 100mb and then uploading each chunk to azure storage (which is not part of this question)
}

现在我已经知道了--我可以使用像--这样的库简单地删除csv文件的头。CSVHelper (将数据写入CSV时,如何排除头文件?)

使用上面的方法,我可以创建一个文件的无头拷贝,并将新文件以FileStream的形式读回来--但问题是,我正在处理大文件,仅仅为了删除头而复制一个文件将是一项耗费空间的工作。

所以第一次--我在StackOverflow上提出一个问题--寻找解决上述问题的好办法。我希望我能够解释清楚这个问题。

c# file-handling csvhelper
1个回答
2
投票

这应该可以搜索到第一行的末尾。

using (var stream = File.OpenRead("~~filepath~~"))
using (var reader = new StreamReader(stream))                
{
    string line = null;
    if ((line = reader.ReadLine()) != null)
    {        
        stream.Position = line.Length + 2;
        // The 2 is for NewLine(\r\n)
    }

    //All my stream handling code of chunking stream into 100mb and then uploading each chunk to azure storage (which is not part of this question)   
}
© www.soinside.com 2019 - 2024. All rights reserved.