使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:17:03  来源:igfitidea点击:

Use Python to write CSV output to STDOUT

pythonpython-3.xstdout

提问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.stdoutis 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 withstatement, because stdoutdoes not have to be opened or closed.

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

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

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

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