Click here to Skip to main content
15,742,120 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I am trying to add a line in my CSV for every post request. However, the first post is fine. The second post suddenly adds 2 rows (the new and the existing one), the third post adds 3 rows etc. How do I fix this?

What I have tried:

def post(self, request, format=None):
        movie = Movie.objects.get(movieId=request.POST['movieId'])
        rating = Rating(ratingValue=request.POST['ratingValue'], movie=movie, user=user)

        with open('data/train.csv', 'a') as csvfile:
            spamwriter = csv.writer(csvfile)
            spamwriter.writerow([user.userID, request.POST['ratingValue'], int(round(time.time(),0))])

        return Response(status=status.HTTP_200_OK)
Updated 17-Oct-16 5:25am
[no name] 16-Oct-16 20:51pm    
"with open('data/train.csv', 'a')", the 'a' means append so your code is doing exactly what you told it to do.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900