Python csv 模块的 writerow() 和 writerows() 方法的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33091980/
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
Difference between writerow() and writerows() methods of Python csv module
提问by Turzo
I am new to the realm of Python. I've been playing with some I/O operations on CSV files lately, and I found two methods in the csvmodule with very similar names - writerow()and writerows(). The difference wasn't very clear to me from the documentation. I tried searching for some examples but they seem to have used them almost interchangeably.
我是 Python 领域的新手。最近我一直在对 CSV 文件进行一些 I/O 操作,我在csv模块中发现了两个名称非常相似的方法-writerow()和writerows(). 从文档中我不太清楚这种差异。我尝试搜索一些示例,但他们似乎几乎可以互换使用它们。
Could anyone help clarify a little bit?
有人可以帮忙澄清一下吗?
采纳答案by horns
writerowtakes an iterable of cells to write:
writerow需要一个可迭代的单元格来写:
writerow(["foo", "bar", "spam"])
->
foo,bar,spam
writerowstakes an iterable of iterables of cells to write:
writerows需要一个可迭代的单元格来编写:
writerows([["foo", "bar", "spam"],
["oof", "rab", "maps"],
["writerow", "isn't", "writerows"]])
->
foo,bar,spam
oof,rab,maps,
writerow,isn't,writerows
So writerowtakes 1-dimensional data (one row), and writerowstakes 2-dimensional data (multiple rows).
所以writerow取一维数据(一行),writerows取二维数据(多行)。
回答by cdonts
writerows(seq)is equivalent to:
writerows(seq)相当于:
for item in seq:
writerow(item)
So the only difference is that writerowslets you pass multiple values!
所以唯一的区别是writerows让你传递多个值!

