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
?
print() is a function. When you call the print() function it returns the value None. writerow(print()) will write None in the CSV file. If you want to print() a string and write the same string to a file you need to create the string, assign it to a variable, print the variable and write the variable to the file.
CSV Dialect defines a simple format to describe the various dialects of CSV files in a language agnostic manner. It aims to deal with a reasonably large subset of the features which differ between dialects, such as terminator strings, quoting rules, escape rules and so on.
Example 1: Read CSV files with csv. reader() is used to read the file, which returns an iterable reader object. The reader object is then iterated using a for loop to print the contents of each row.
On the Data tab, in the Get & Transform Data group, click From Text/CSV. In the Import Data dialog box, locate and double-click the text file that you want to import, and click Import. In the preview dialog box, you have several options: Select Load if you want to load the data directly to a new worksheet.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With