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