How do I create a multiline cell based CSV file within Python ?
Within this short tutorial we will show you the steps required to create a csv file that includes multi-line cells within Python.
In order to create a mutliline cell within Excel (2010/2013) the following is required:
- newlines are represented with a carriage return/newline,
- the string should be wrapped in quotes.
One of the great things with the Python csv module is it takes care of the quotes for you. Below provides an example,
multiline_string = "this\nis\nsome\ntext" # assign string
multiline_string = multiline_string.replace('\n','\r\n') # convert newlines to newlines+carriage return
with open('xyz.csv', 'wb') as outfile:
w = csv.writer(outfile) # assign csv writer method
w.writerow(['sometext',multiline_string]) # append/write row to file