【问题标题】:Manipulating csv structure and content with csvhelper使用 csvhelper 操作 csv 结构和内容
【发布时间】:2020-12-31 16:24:25
【问题描述】:

我一直在尝试通读一些存储在 blob 存储中的 csv 文件,并附加和填充两列。我正在使用 csvhelper 来执行此操作,但在尝试修改实际行时发现它很困难。

我希望这个过程是动态的,因为我将循环遍历容器路径,并且我手头没有所有 csv 文件的结构。

  using (StreamReader reader = new StreamReader(blob.OpenRead()))
    using (CsvReader csv = new CsvReader(reader))
    {
     csv.Read();
     csv.ReadHeader();
     List<string> headers = csv.Context.HeaderRecord.ToList();
     headers.Add("ColA");
     headers.Add("ColB");
     newHeaderContent = string.Join(",", headers);

     // Not sure how to read through the csv and populate the two columns I just appended

    }

using (StreamWriter writer = new StreamWriter(processedblob.OpenWrite()))
    using (CsvWriter csvwriter = new CsvWriter(writer) )
        {
            writer.WriteLine(String.Concat(newHeaderContent));
            //writer code for rows
        }

【问题讨论】:

  • 你的问题是什么?
  • @MightyBadaboom 无法读取/循环现有行并将 2 个新值附加到每行的末尾。

标签: c# azure csv azure-blob-storage csvhelper


【解决方案1】:

根据issue,CsvHelper 不支持这种编辑功能。他还提到您需要 2 个文件,从旧文件中读取(然后进行一些更改),然后将更新写入新文件。

下面的代码可以运行并遵循上述说明:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;
using System.Text;

namespace AzureBlobConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("your_storage_account", "storage_account_key"), true);
            CloudBlobClient client = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = client.GetContainerReference("f11");
            CloudBlockBlob blob = blobContainer.GetBlockBlobReference("t2.csv");

            //download the .csv file into a text
            string s1 = blob.DownloadText();

            //split the text into an array
            string[] temp = s1.Split('\r');

            // variable s used to store the updated text from .csv
            string s = "";

            //because the last element of the array is redundant data("\n"), so we use temp.Length-1 to abandon it.
            for (int i=0;i<temp.Length-1;i++)
            {
                if (i == 0)
                {
                    temp[i] += ",ColA,ColB"+"\r\n";
                }
                else
                {
                    temp[i] += ",cc,dd"+"\r\n";
                }

                s += temp[i];
            }

            //upload the updated .csv file into azure
            var stream = new MemoryStream(Encoding.UTF8.GetBytes(s));
            blob.UploadFromStream(stream);         

            Console.WriteLine("completed.");
            Console.ReadLine();
        }      

    }
}

测试结果:

更新前:

更新后:

【讨论】:

  • 谢谢@Ivan。我最终加载到数据表并在那里进行操作,但这更优雅。谢谢
猜你喜欢
  • 2022-01-05
  • 2020-07-08
  • 2021-10-02
  • 1970-01-01
  • 2019-01-23
  • 2020-03-19
  • 2011-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多