[英]write file in list and save as .txt or .doc in python
I try to play with number while practice python and i try to save it as a text or word document, but when saved, the file doesn't have a format, and i think python default saved it as a text file (.txt) but im wrong its saved like a unknown format file 我在练习python时尝试使用数字,并尝试将其保存为文本或Word文档,但是保存后,文件没有格式,我认为python default将其保存为文本文件(.txt)但我错误地将其保存为未知格式的文件
this is my code 这是我的代码
def Calculator(self):
input1 = self.ui.lineEdit1.text()
input2 = self.ui.lineEdit2.text()
compare = ''
if input1 == input2:
compare = 'Yes its Same Number'
else:
compare = 'You input different number'
self.ui.textBrowser.setPlainText(compare)
def save(self, savein):
with open(savein, 'w') as f:
f.write( 'Number 1 :' + str(self.ui.lineEdit1.text()) )
f.write( 'Number 2 :' + str(self.ui.lineEdit2.text()) )
f.write( 'Conclusion :' + str(self.ui.textBrowser.toPlainText()) )
f.close()
def savefile(self):
if self.savein:
self.save( "%s" % self.savein )
else:
self.saveAs()
def saveAs(self):
tulis = QtGui.QFileDialog(self).getSaveFileName()
if filename !="":
_filename = "%s" % filename
self.save( _filename )
and when i try to open it with notepad it writen in one line, like this: 当我尝试用记事本打开它时,它写在一行中,如下所示:
Number 1 :20000Input 2 :3000000Conclusion :You input different number
what must i add so it save output as a list, like this: 我必须添加什么,以便将输出保存为列表,如下所示:
Number 1:20000
Number 2:30000
Conclusion : Different
Thanks 谢谢
add \\n
at the end of the line, for example, change: 在行的末尾添加\\n
,例如,更改:
f.write( 'Number 1 :' + str(self.ui.lineEdit1.text()) )
to: 至:
f.write( 'Number 1 :' + str(self.ui.lineEdit1.text()) + '\n')
etc. 等等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.