-1

I want to add data into a CSV file. User will enter the value via a PYQT5's line edit input widget. When user click on "add record", the CSV file gets updated.

GUI Layout:

enter image description here

Code Snippet I've tried:

    def addRecord(self):

        list = [self.lineEdit.text(), ',', self.lineEdit_3.text()]
        with open('player_highscore.csv','a') as f_object:

            writer_object = writer(f_object)
            writer_object.writerow(list)

            f_object.close()

With this code, I've tried placing the value 1 for each lineEdit, and have clicked the button twice. This is the output:

LVL,Highscore
10,979161,",",1

1,",",1

How do I resolve this issue? The desired result is:

LVL,Highscore
10,979161
1,1
1,1
  • For start, don't use `list` as name. Then post [mre]. Are you using `csv` module? – buran Oct 09 '21 at 10:06
  • Is this respond to your question : https://stackoverflow.com/questions/2084069/create-a-csv-file-with-values-from-a-python-list ? If you want to append things in the csv use 'a' in your open. Here the official documentation of open on python 3 : https://docs.python.org/3/library/functions.html#open – Da2ny Oct 09 '21 at 11:07

1 Answers1

0

It seems the , will be added automatically without writing it.
Try to write this list, instead:

list = [self.lineEdit.text(), self.lineEdit_3.text()]