0

I fill in the table as any data and then export the file (CSV). OK so far. But when I open the CSV file I notice empty rows between the rows I filled. How to solve it? Run the code, fill the table, export and see the file to understand.

from tkintertable import TableCanvas, TableModel
from tkinter import *

root=Tk()
t_frame=Frame(root)
t_frame.pack(fill='both', expand=True)

table = TableCanvas(t_frame, cellwidth=60, thefont=('Arial',12),rowheight=18, rowheaderwidth=30,
                    rowselectedcolor='yellow', editable=True)

table.show()

root.mainloop()

look the empty rows in the CSV file

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Leonardo
  • 120
  • 9
  • 1
    I have a solution that will fix your problem. Please see my updated answer. – Mike - SMT Dec 23 '19 at 13:43
  • Wow!!! It worked! Fantastic work! Thank you so much! – Leonardo Dec 23 '19 at 14:27
  • 1
    Glad to help. If my post has solved your problem please select the check mark next to the answer to show your issue has been resolved. – Mike - SMT Dec 23 '19 at 14:28
  • Please do not add conversational material into posts. There is a general expectation that technical writing is adhered to here. Thanks! – halfer Jan 01 '20 at 23:39
  • Leonardo, please stop editing back in the chatty material at the end of the post. There is community consensus that such material does not belong on this site -- https://meta.stackoverflow.com/questions/260776/should-i-remove-fluff-when-editing-questions . – josliber Jan 07 '20 at 02:56

1 Answers1

2

If you open the CSV file in notepad or some other basic text editor you will see that the data is saved with spaces in between each row of data. Because EXCEL reads a CSV per new line then it will always import this way into EXCEL.

Only things you can really do is manually going in and removing the lines from the CSV, building a macro to clean it up for you after the fact or editing the tkintertable library where the issue is occurring.

The Usage page of the GitHub for tkintertable does not appear to have any details on exporting to CSV that will fix this issue. Just a problem with how this library works with data I guess.

Even adding a Button that runs a command to export you get the same problem.

def export_to_csv():
    table.exportTable()

Button(root, text='test CSV', command=export_to_csv).pack()

Usage

enter image description here

UPDATE:

After doing some digging and realizing the tkintertable uses the csv librarty to write to csv I did some googling on the same problem with writing to csv using the csv library and found this post (csv.write skipping lines when writing to csv). With that post I did some digging in the tkintertable library to find where the writing occurred and here is what I found.

The writing of new lines in between data appears to be a result of how the csv library is writing data. If we dig in you will find that the tkintertable class that is writing your table to CSV is called ExportTableData. That class is as follows:

class TableExporter:
    def __init__(self):
        """Provides export utility methods for the Table and Table Model classes"""

        return

    def ExportTableData(self, table, sep=None):
        """Export table data to a comma separated file"""

        parent=table.parentframe
        filename = filedialog.asksaveasfilename(parent=parent,defaultextension='.csv',
                                                  filetypes=[("CSV files","*.csv")] )
        if not filename:
            return
        if sep == None:
            sep = ','
        writer = csv.writer(open(filename, "w"), delimiter=sep)
        model=table.getModel()
        recs = model.getAllCells()
        #take column labels as field names
        colnames = model.columnNames
        collabels = model.columnlabels
        row=[]
        for c in colnames:
            row.append(collabels[c])
        writer.writerow(row)
        for row in recs.keys():
            print(row)
            writer.writerow(recs[row])
        return

If we update this line:

writer = csv.writer(open(filename, "w"), delimiter=sep)

To include lineterminator = '\n' you will find that the problem goes away:

writer = csv.writer(open(filename, "w"), delimiter=sep, lineterminator = '\n')

Results:

enter image description here

Keep in mind editing libraries is probably not a great idea but for this situation I think it is your only option.

To get to this class you will need to open the python file called Tables_IO.py in the tkintertable library.

If you are using PyCharm you can navigate through the library files with CTRL+Left Click.

First Ctrl+Click on the import name enter image description here. Then you will Ctrl+Click on the import name enter image description here. Then you will search for a class method called exportTable and inside that method you will Ctrl+Click enter image description here. This will take you to the above mentioned class that you can edit to solve the problem.

Please take care not to edit anything else as you can very easily break your library and will need to reinstall it if that happens.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79