Add columns to CSV while writing the CSV

前端 未结 2 661
感动是毒
感动是毒 2020-12-07 04:57

I am writing on the fly the following data in a csv:

name first file parsed                    
STEP ID  ELEMENT_ID  Fatigue SW  Fatigue F1  Fat         


        
2条回答
  •  独厮守ぢ
    2020-12-07 05:50

    1. Define a class that represents the original row of data (such as OriginalData).
    2. Define a second class that derives from the first class, and includes properties for each of the new columns (such as NewData).
    3. Create a constructor on NewData that takes an OriginalData as an argument. Have it copy the data from OriginalData into itself.
    4. Overload ToString() on NewData so that it returns a string in the format that you want it to appear in the target file.
    5. While you're iterating over the lines, read them into an OriginalData instance.
    6. Once the originalData instance is loaded, copy the data into a NewData instance, and populate the new properties to include your data.
    7. Write the data from NewData to the target file by calling NewData's ToString() method.

提交回复
热议问题