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 takes an iterable of cells to write:
writerow(["foo", "bar", "spam"])
->
foo,bar,spam
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
So writerow takes 1-dimensional data (one row), and writerows takes 2-dimensional data (multiple rows).
所有评论(0)