Home > Software design >  Adding Multiple values to a single row in a csv file in python
Adding Multiple values to a single row in a csv file in python

Time:12-09

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

CodePudding user response:

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()
    ])
  • Related