Python CSV模块的writerow()和writerows()方法之间的区别 [英] Difference between writerow() and writerows() methods of Python csv module
问题描述
我是Python领域的新手.最近,我一直在对CSV文件进行一些I/O操作,并且在csv
模块中找到了两个名称非常相似的方法-writerow()
和writerows()
.从文档中我对区别不是很清楚.我尝试搜索一些示例,但它们似乎几乎可以互换使用.
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 csv
module 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.
有人可以帮忙澄清一下吗?
Could anyone help clarify a little bit?
推荐答案
writerow
使用可迭代的单元格来编写:
writerow
takes an iterable of cells to write:
writerow(["foo", "bar", "spam"])
->
foo,bar,spam
writerows
进行一个可迭代的单元格迭代:
writerows
takes an iterable of iterables of cells to write:
writerows([["foo", "bar", "spam"],
["oof", "rab", "maps"],
["writerow", "isn't", "writerows"]])
->
foo,bar,spam
oof,rab,maps,
writerow,isn't,writerows
所以writerow
接受一维数据(一行),而writerows
接受二维数据(多行).
So writerow
takes 1-dimensional data (one row), and writerows
takes 2-dimensional data (multiple rows).
这篇关于Python CSV模块的writerow()和writerows()方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!