How to write CSV output to stdout?

Question:

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?

Asked By: jsf80238

||

Answers:

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)
Answered By: Lev Levitsky
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.