0

I extracted text from a QTextBrowser and now I want to save this as csv. But each letter is saved in each row in the csv file. How can I get each line of the text to print in each row. The text looks like this:

'Input data\nEnergy (kWh): 1,234\nPower (kWh/y): 12345.0\nLength, Width (m): 2.0, 1.0\n---\nOutput data\nBuildings: 123\nTotal power generation (MWh): 1,234\n\n'

My code:

import csv
with open('path/to/output.csv', 'w') as csvfile:
    text = self.dockwidget.textBrowser.toPlainText()
    writer = csv.writer(csvfile, delimiter='\n')
    for lines in text:
        writer.writerow([lines])

I want the csv file to look like:

Input data
Energy (kWh): 1,234
Power (kWh/y): 12345.0
Length, Width (m): 2.0, 1.0
---
Output data
Buildings: 123
Total power generation (MWh): 1,234
jim
  • 63
  • 7
  • Can you give a clear input and output specification? Why do you expect the program to do anything else than writing everything on one line when you use `writerow([text])`? – Tomerikoo Jul 12 '22 at 15:12
  • Can you please replace `text` with whatever it is `self.dockwidget.textBrowser.toPlainText()` is returning? I don't see how the question is actually related to QT – Tomerikoo Jul 12 '22 at 15:19
  • Simply changing to `for lines in text.splitlines():` might be what you're looking for. But anyway this is not really a csv format. Why are you writing a csv file and not a regular text file? – Tomerikoo Jul 12 '22 at 15:21

0 Answers0