How to write CSV output to stdout?

PythonPython 3.xStdout

Python Problem Overview


I know I can write a CSV file with something like:

with open('some.csv', 'w', newline='') as f:

How would I instead write that output to stdout?

Python Solutions


Solution 1 - Python

sys.stdout is a file object corresponding to the program's standard output. You can use its write() method. Note that it's probably not necessary to use the with statement, because stdout does not have to be opened or closed.

So, if you need to create a csv.writer object, you can just say:

import sys
spamwriter = csv.writer(sys.stdout)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjsf80238View Question on Stackoverflow
Solution 1 - PythonLev LevitskyView Answer on Stackoverflow