更新时间:2023-12-04 11:14:22
参见 13.1。 csv - CSV文件读取和写入 - Python 2.7.12文档 [ ^ ]。
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):
user=MyUser.objects.get(user=request.user)
movie = Movie.objects.get(movieId=request.POST['movieId'])
rating = Rating(ratingValue=request.POST['ratingValue'], movie=movie, user=user)
rating.save()
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)
See 13.1. csv — CSV File Reading and Writing — Python 2.7.12 documentation[^].