Python Forum
[PyQt] saving text file by FileDialog option
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] saving text file by FileDialog option
#11
this is not a question that i want the code ready, but i'm new to python, and i'm trying to improve my knowledge on the net.
about my code, i just changed it for a more readable appearance of the text file.

def create_file_from_tablewidget(self):
  
    header = []
    lenght=12
    for column in range(self.tableWidget.columnCount()):      
        h = self.tableWidget.horizontalHeaderItem(column).text()
        header.append(h+'\t')
                  
    path,_ = QFileDialog.getSaveFileName(
        self, 'Save File', '', 'CSV(*.txt)')
    if path:
        with open(path, 'w',encoding="utf-8")  as stream:
            writer = csv.writer(stream, delimiter='\t')
            writer.writerow(header)
            for row in range(self.tableWidget.rowCount()):
                rowdata = []
                for column in range(self.tableWidget.columnCount()):
                    item = self.tableWidget.item(row, column)
                    if item is not None:
                        rowdata.append(item.text()+" " * (lenght - len(item.text())))
                    else:
                        rowdata.append('')
                writer.writerow(rowdata)
there is the new apparence of the text file :

[Image: i7ts.jpg]
Reply
#12
Well that does look nicer -- and if you can remove those unnecessary quotes at the top it would look even nicer... if you want to put those | and - in as you had shown in the other that would be pretty easy to do with the methodology (pseudo-code) I outlined. So no questions about that methodology then?
Reply
#13
hi;
I remove the '\' character and replace it by ' ' at the line 7 of the code.
header.append(h+'           ')
the new appearance of the text file :

[Image: asl0.jpg]
Reply
#14
You can write your table to a tab delimited csv, for example "/tmp/test.csv" then (change expandtabs(32) to your needs)

infile = "/tmp/test.csv"
outfile = "/tmp/test.txt"

with open(infile, 'r') as stream:
    text = stream.readlines()
    with open(outfile, 'w') as f:
        for line in text:
            print (line.expandtabs(32), end = "", file=f)
Reply
#15
thanks for your help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Saving file to html or pdf stopped working in PyQt6 ejKDE 4 275 Mar-12-2024, 07:45 PM
Last Post: ejKDE
  [Tkinter] filedialog, open a file error liketocode 4 3,232 Dec-07-2022, 10:51 PM
Last Post: liketocode
  [Tkinter] logical error - saving file rwahdan 4 2,045 Jul-01-2021, 12:59 PM
Last Post: rwahdan
  [Tkinter] Exclude hidden file, filedialog.askopenfile red380sl 1 3,117 Dec-01-2020, 07:04 PM
Last Post: DT2000
  [Tkinter] Unable to save filepath to config.ini file using filedialog.askopenfilename JackMack118 10 4,898 Dec-29-2019, 08:12 PM
Last Post: JackMack118
  Part of code is adding extra new line when saving to text file. lovepeace 9 4,868 Aug-24-2019, 12:52 PM
Last Post: lovepeace
  Update value selected in option menu when select an item from text box klllmmm 2 4,953 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  tkinter filedialog and pickle - custom icon question. kim07133 0 2,729 Jan-08-2018, 12:10 PM
Last Post: kim07133
  [Tkinter] filedialog. FULL SCREEN issac_n 0 3,114 Dec-05-2017, 07:33 AM
Last Post: issac_n

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020