Python CSV模块的writerow()和writerows()方法之间的区别 [英] Difference between writerow() and writerows() methods of Python csv module

查看:1810
本文介绍了Python CSV模块的writerow()和writerows()方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆