Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding Multiple values to a single row in a csv file in python

I am trying to add multiple column values to a single row in python, i have tried this:

with open (filename,'a') as golds:
        golds.write(self.BOBtextbox.get())
        golds.write(self.Whompstextbox.get())
        golds.write(self.SSLtextbox.get())
        golds.write(self.BITDWtextbox.get())
        golds.write(self.SSLtextbox.get())
        golds.write(self.LLLtextbox.get())
        golds.write(self.HMCtextbox.get())
        golds.write(self.DDDtextbox.get())
        golds.write(self.BITFStextbox.get())
        golds.write(self.BLJtextbox.get())
        golds.write(self.BITStextbox.get())

However this adds all the data to a single row and column whereas i want all each write statement in a different column, any ideas?

Edit: just to be clearer, the value outputted into the csv file is put all into one cell:
1254567891011
instead of:
1,2,3,4,5,6,7,8,9,10,11

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming there are no commas in all of the text boxes string, have you tried adding comma between each expected column?

with open (filename,'a') as golds:
    golds.write(self.BOBtextbox.get())
    golds.write(",")
    golds.write(self.Whompstextbox.get())
    golds.write(",")
    golds.write(self.SSLtextbox.get())
    golds.write(",")
    golds.write(self.BITDWtextbox.get())
    golds.write(",")
    golds.write(self.SSLtextbox.get())
    golds.write(",")
    golds.write(self.LLLtextbox.get())
    golds.write(",")
    golds.write(self.HMCtextbox.get())
    golds.write(",")
    golds.write(self.DDDtextbox.get())
    golds.write(",")
    golds.write(self.BITFStextbox.get())
    golds.write(",")
    golds.write(self.BLJtextbox.get())
    golds.write(",")
    golds.write(self.BITStextbox.get())

Alternatively, you can use python csv built-in library instead

import csv
with open(filename, 'a', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerow([
        self.BOBtextbox.get(),
        self.Whompstextbox.get(),
        self.SSLtextbox.get(),
        self.BITDWtextbox.get(),
        self.SSLtextbox.get(),
        self.LLLtextbox.get(),
        self.HMCtextbox.get(),
        self.DDDtextbox.get(),
        self.BITFStextbox.get(),
        self.BLJtextbox.get(),
        self.BITStextbox.get()
    ])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading