Python Forum

Full Version: How to save data into next columns
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi,

I am writing a csv file which appends data into rows like [attachment=483] but want it like this [attachment=484]
I have list [] where i am appending the data and using it to to write file :

def writefiles(alldata, filename):
        with open ("./"+ filename, "w") as csvfile:
            csvfile = csv.writer(csvfile, delimiter=",")
            csvfile.writerow("")
            for i in range(0, len(alldata)):
                csvfile.writerow(alldata[i])

writefiles(alldata, "Data.csv")
How can i do this?
It's about how you construct your data.

>>> import csv
>>> data = [['a', 123, 123,123], ['b', 345, 345, 345]]
>>> with open('/tmp/data.csv', 'w') as output:
...     writer = csv.writer(output)
...     for row in data:
...         writer.writerow(row)
... 
15
15
>>> with open('/tmp/data.csv', 'r') as data:
...     for line in data:
...         print(line)
... 
a,123,123,123

b,345,345,345

>>> 
Oh no no.....i am appending data into csv and you have converted them to list , my columns are not in list they are just simple columns, how to convert them to list like you did it?
Read the data, convert it as you want and write it back.
Just add to the first list as long as the first element of the data is 'a'. Same with the 'b's