Why is the separator '\ n'.join () not working, the elements of the list are output to the file.txt in a row one by one, without a space. And should each element of the list begin with a new line. When I remove the tags, everything works.

tx1 = text1.toPlainText() tx2 = text2.toPlainText() rows1 = tx1.split('\n') rows2 = tx2.split('\n') for i in range(min_len): if rows1[i] != rows2[i]: str1='<span style="background:#99FF66">'+rows1[i]+'</span>' str2='<span style="background:red">'+rows2[i]+'</span>' rows1[i] = str1 rows2[i] = str2 txt1 = '\n'.join([str(j) for j in rows1]) txt2 = '\n'.join([str(j) for j in rows2]) text1.setText(txt1) text2.setText(txt2) 
  • Give an example of the source files where you get the text, because, for example, it works fine for me if you put tx2 own text in tx1 and tx2 . Maybe your text editor doesn’t see \n as a line break, try \r\n , or open it in another editor. - Flowneee
  • Checked with different, any * .txt files. None of them work. '\ r \ n' also does not see. When I write: '"\ n"' .join, between the elements of the list appears: "". - Teit
  • And what about toPlainText and setText ? Maybe the problem is in them, because if you open files with normal ones and read / write with standard Python functions, there are no problems. - Flowneee
  • text1 = QtGui.QTextEdit (txt, parent) is a multi-line Gui pyqt4 text field. text1.setText - fill this field with text. text1.toPlainText - get the text that is already loaded in this text field. Yes, there are no problems with standard features. So far, only with this field. - Teit
  • and if then each line is added separately? something like this: for i in rows1: text1.append(str(i)) - Flowneee

1 answer 1

Transferred from comments

Add each line to TextEdit separately using the append method:

 for i in rows1: text1.append(str(i)) 

And similarly for the 2nd list of strings.