Python Forum
How to save data into next columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save data into next columns
#1
hi,

I am writing a csv file which appends data into rows like     but want it like this    
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?
Reply
#2
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

>>> 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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?
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 555 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Extracting Data into Columns using pdfplumber arvin 17 5,582 Dec-17-2022, 11:59 AM
Last Post: arvin
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,881 Dec-12-2022, 08:22 PM
Last Post: jh67
  How to keep columns header on excel without change after export data to excel file? ahmedbarbary 0 1,164 May-03-2022, 05:46 PM
Last Post: ahmedbarbary
  Save data frame to .csv df.to.csv() mcva 1 1,543 Feb-03-2022, 07:05 PM
Last Post: mcva
  Python Pandas: How do I average ONLY the data >1000 from several columns? JaneTan 0 1,480 Jul-17-2021, 01:34 PM
Last Post: JaneTan
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,168 Jul-02-2021, 02:19 PM
Last Post: xtc14
  [Solved] Using readlines to read data file and sum columns Laplace12 4 3,559 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  How to save json data in a dataframe shantanu97 1 2,162 Apr-15-2021, 02:44 PM
Last Post: klllmmm
  Pandas: how to split one row of data to multiple rows and columns in Python GerardMoussendo 4 6,836 Feb-22-2021, 06:51 PM
Last Post: eddywinch82

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020