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()
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
tkintertablelibrary where the issue is occurring.The Usage page of the GitHub for
tkintertabledoes 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.
Usage
UPDATE:
After doing some digging and realizing the
tkintertableuses thecsvlibrarty to write to csv I did some googling on the same problem with writing to csv using thecsvlibrary and found this post (csv.write skipping lines when writing to csv). With that post I did some digging in thetkintertablelibrary 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
csvlibrary is writing data. If we dig in you will find that thetkintertableclass that is writing your table to CSV is calledExportTableData. That class is as follows:If we update this line:
To include
lineterminator = '\n'you will find that the problem goes away:Results:
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.pyin thetkintertablelibrary.If you are using PyCharm you can navigate through the library files with CTRL+Left Click.
First Ctrl+Click on the import name
. Then you will Ctrl+Click on the import name
. Then you will search for a class method called
. This will take you to the above mentioned class that you can edit to solve the problem.
exportTableand inside that method you will Ctrl+ClickPlease take care not to edit anything else as you can very easily break your library and will need to reinstall it if that happens.