使用Python将CSV输出写入STDOUT

时间:2022-07-01 05:49:14

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

我知道我可以用以下内容写一个CSV文件:

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

How would I instead write that output to STDOUT?

我该如何将该输出写入STDOUT?

1 个解决方案

#1


60  

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.

sys.stdout是与程序标准输出对应的文件对象。您可以使用其write()方法。请注意,可能没有必要使用with语句,因为不必打开或关闭stdout。

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

所以,如果你需要创建一个csv.writer对象,你可以说:

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

#1


60  

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.

sys.stdout是与程序标准输出对应的文件对象。您可以使用其write()方法。请注意,可能没有必要使用with语句,因为不必打开或关闭stdout。

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

所以,如果你需要创建一个csv.writer对象,你可以说:

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