Skip to content Skip to sidebar Skip to footer

Appending Data To Csv File

I am trying to append 2 data sets to my csv file. Below is my code. The code runs but my data gets appended below a set of data in the first column (i.e. col[0]). I would however l

Solution 1:

I think I see what you are trying to do. I won't try to rewrite your function entirely for you, but here's a tip: assuming you are dealing with a manageable size of dataset, try reading your entire CSV into memory as a list of lists (or list of tuples), then perform your calculations on the values on this object, then write the python object back out to the new CSV in a separate block of code. You may find this article or this one of use. Naturally the official documentation should be helpful too.

Also, I would suggest using different files for input and output to make your life easier.

For example:

import csv
data = []
with open('Trial_test.csv', 'rb') as csvfile:
   reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
   for row in reader:
       data.append(row)

# now do your calculations on the 'data'object.

with open('Trial_test_new.csv', 'wb') as csvfile:
   writer = csv.writer(csvfile, delimiter=' ', quotechar='|')
   for row indata:
       writer.writerow(row)

Something like that, anyway!

Post a Comment for "Appending Data To Csv File"