with open('output.csv', 'w', newline='', encoding='utf-8') as f: writer = csv.writer(f) ...
On Windows, always open your files in binary mode (
"wb"), before passing them to
Although the file is a text file, CSV is regarded a binary format by the libraries involved, with
\r\n separating records. If that separator is written in text mode, the Python runtime replaces the
\r\n, hence the
\r\r\n observed in the file.
See this previous answer.