python3 csv.writerows() puts cr after each row on Linux

Viewed 49

Many people have asked why csv.writerows() produces \r\n\n on windows, and have been advised to open the file with newline=''. But on Linux, my problem is the reverse, I get \r\n when I wanted \n and I can't get rid of the carriage return

#!/usr/bin/python3
  
import csv

with open("position.csv", 'w',newline='') as file:
  writer = csv.writer(file,quoting=csv.QUOTE_NONNUMERIC)
  lat=3.7
  long=4.5
  writer.writerow(['coords', lat,long])

od -cd position.csv 
0000000   "   c   o   o   r   d   s   "   ,   3   .   7   ,   4   .   5
          25378   28527   25714    8819   13100   14126   13356   13614
0000020  \r  \n
           2573
0000022

If I open with 'wb', then it complains I am writing strings, not binary

1 Answers
  
import csv

with open("position.csv", 'w',newline='') as file:
  writer = csv.writer(file,quoting=csv.QUOTE_NONNUMERIC,lineterminator='\n')
  lat=3.7
  long=4.5
  writer.writerow(['coords', lat,long])
Related