两者之间的区别csv.reader和。导入csv fi时读取

2024-05-23 22:02:37 发布

您现在位置:Python中文网/ 问答频道 /正文

用reader和.read导入csv文件有什么区别

import csv
f = open("nfl.csv", 'r')
data = csv.reader(f)

并直接使用read

^{pr2}$

Tags: 文件csvimportreaddataopenreadernfl
1条回答
网友
1楼 · 发布于 2024-05-23 22:02:37

docsreader

Return a reader object which will iterate over lines in the given csvfile.

而文件上的read

reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory.

所以,第一种方法,你可以用

for row in reader: 

一次处理一条线。 一般来说,您也可以对一个文件执行一行操作。在

csv模块需要逗号分隔的列,因此您可以根据设置的方式获得数据的列表或字典。在

相关问题 更多 >