0

Im trying to append user inputted data to rows , however im having no luck.

Counter is a user defined global variable which i have created elsewhere in the code.

when the data is appended, instead of moving to another line it just rewrites the same line over and over and as a new user of python i have no idea what im doing wrong.

def AppendCsv(): 
    NumberOfNamesToBeAdded()
    with open ('Graph.csv', 'a') as graphFile:
        graphFileWriter=csv.writer(graphFile)
        for x in counter:
             graphFileWriter.writerow([ID, Name,Cost,Date1]) 
3
  • 1
    What is ID, Name, Cost and Date1? Do they change on these loops? Commented Dec 1, 2017 at 21:07
  • 1
    When you say it rewrites the same line over and over, do you mean it overwrites the same line and you end up only appending a single new row to the CSV? Or do you mean that the same row is appended multiple times? Commented Dec 1, 2017 at 21:11
  • I mean that the same row is overwritten, so only 1 row ends up being appended Commented Dec 2, 2017 at 9:38

1 Answer 1

1

It depends what's in counter, for one thing. With a range in counter, like this:

import csv

counter = range(3)

with open('Graph.csv', 'a', newline='') as graphFile:
    graphFileWriter = csv.writer(graphFile)
    for x in counter:
        graphFileWriter.writerow([1,2,3])

the result is this:

1,2,3
1,2,3
1,2,3

You might also want to insert , newline='' in the open statement.

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

2 Comments

counter is a string , so if i just change it to counter=range(input('enter the number')) this should work?
I expect so, yes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.