Wednesday 21 October 2020

'\r' not working as `lineterminator` within Python `csv.writer()`

I'm working on Windows. I've a Python file to create a new CSV file and I view that using notepad (even through Ms Excel).

import csv
data=[['fruit','quantity'],['apple',5],['banana',7],['mango',8]]
with open('d:\lineter.csv','w') as l:
    w=csv.writer(l,delimiter='|',lineterminator='\r')
    w.writerows(data)

The resulted file in notepad :

fruit|quantityapple|5banana|7mango|8

My doubt here is whether the carriage return \r works or not??? It works like lineterminator='' in notepad. But in excel, it works like '\n'

The output doesn't seem to implement carriage return. When I use lineterminator as :

w=csv.writer(l,delimiter='|',lineterminator='*\r*\n')

The output in notepad is:

fruit|quantity**
apple|5**
banana|7**
mango|8**

This is evident here too.

Can anyone tell me how '\r' works in lineterminator in writer()? Or is there any other thing happening there?



from '\r' not working as `lineterminator` within Python `csv.writer()`

No comments:

Post a Comment