使用 Python 将 CSV 输出写入 STDOUT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23665264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use Python to write CSV output to STDOUT
提问by jsf80238
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?
采纳答案by Lev Levitsky
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)