I've been trying to read through some csv files that are stored in blob storage and append and populate two columns. I'm using csvhelper to do this but finding it difficult when trying to modify the actual rows.
I would like this process to be dynamic as I'll be looping through a containers paths, and I do not have the structure of all the csv files at hand.
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
}
As per this issue, CsvHelper does not support this kind of edit feature. And he also mentions that you need 2 files, read from the old one(and then make some changes), then write the updates to the new one.
The code below works and following the above instructions:
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();
}
}
}
Test result:
Before update:
After update: