0

I'm reading and writing some csv files that are stored in Azure Blob Storage.

The files have headers and I would like to add 2 additional columns with defined headers and values to the files and write them to another location. I'm using StreamReader and StreamWriter to read and write to the files, but was having difficulty in appending like I normally would do with the File.ReadLines method.

The new columns and associated values are what I would like written to a new blob, depicted below.


|Column A|Column B|Column C | New Column 1 | New Column 2 |
|--------|--------|---------|--------------|--------------|
|   1    |   2    |   3     |   new value  |  new value   |   
|   4    |   5    |   6     |   new value  |  new value   |     

*EDIT -- Added Code below that highlights what functionality I would like, but I'm restricted due to working with streams

            var csv = 
                File.ReadLines(rawfilePath).Select((line, index) => index == 0
              ? line + ",Order"
              : line + "," + index.ToString())
           .ToList();

            File.WriteAllLines(processedfilePath, csv);
user552311
  • 39
  • 7

1 Answers1

0

I think it is much easier to operate with classes, you can use any lib that works with CSV (de)serialization and do next way:

  • create class that represents a row
  • add columns (properties to class) you want
  • download and deserialize CSV to class objects array
  • fill new columns with data (they would have null or default value after deserialization)
  • serialize objects array to CSV and upload file

Here is an example:

https://stackoverflow.com/a/2094885/10496806

Woldemar89
  • 682
  • 4
  • 10