1

I'm trying to write a cmd program to track the updated value of investments and things. I want to save their value in a CSV file using csv library and have a history of their values over time. there are some constant investments like ['Gold','Stock','Bit-coin'] and I have added them as default header in my program like this:

+------------+-------------+------------+
|    Date    |  Stock | Gold | Bit-coin |
+------------+-------------+------------+

But I want my program to have the feature for adding Other categories as the user wants to name it. and add and also edit the header whenever wants to edit it. Is there any way to dynamically edit header names as new column data is added to the CSV file?

1
  • 1
    You could do this, but instead of just appending the CSV file anytime you had new data, you would have to re-write the whole file with a new header and fill-ins (probably n/a's or such) in the columns up to the date off change of the header, right? So an alternative might be just to make many csv files, one for each commodity or item and it is easy to have python walk a whole directory of them an ingest them if they have a standard format Commented Nov 27, 2020 at 16:16

1 Answer 1

1
import csv

with open('C:/test/test.csv','r') as csvinput:
    with open('C:/test/output.csv', 'w') as csvoutput:
        writer = csv.writer(csvoutput, lineterminator='\n')
        reader = csv.reader(csvinput)

        all = []
        row = next(reader)
        row.append('Berry')
        all.append(row)

        for row in reader:
            row.append(row[0])
            all.append(row)

        writer.writerows(all)

Here you can append a new column to your csv file

new_column = input("Enter a new column name:")

Now with this line you can take user input.

You should be able to complete the code on your own, by the way, when posting a question please post the code you're working with so we can grab it and display what you might be requiring with a solution.

Sign up to request clarification or add additional context in comments.

1 Comment

tnx for your answer. I will consider your suggestion for improving my questions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.