简体   繁体   English

如何将 CSV 输出写入标准输出?

[英]How to write CSV output to stdout?

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

sys.stdout is a file object corresponding to the program's standard output. sys.stdout是对应于程序标准输出的文件对象。 You can use its write() method.您可以使用它的write()方法。 Note that it's probably not necessary to use the with statement, because stdout does not have to be opened or closed.请注意,可能没有必要使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM