简体   繁体   English

python 的 csv.reader 是否将整个文件读入内存?

[英]Does python's csv.reader read the entire file into memory?

Does the csvreader object read the entire file into memory? csvreader 对象是否将整个文件读入内存? If I have big data, would it crash because of low memory.如果我有大数据,它会不会因为内存不足而崩溃。 Or it's only a pointer so that I can process each line?或者它只是一个指针,以便我可以处理每一行?

import csv
with open('RawData.csv','r') as file:
    csvreader = csv.reader(file, delimiter=',')
    for row in csvreader:
        print(row)

From the csv.reader documentation:csv.reader文档:

Return a reader object which will iterate over lines in the given csvfile.返回一个 reader 对象,该对象将遍历给定 csvfile 中的行。 csvfile can be any object which supports the iterator protocol and returns a string each time its __next__() method is called — file objects and list objects are both suitable. csvfile 可以是任何支持迭代器协议并在每次调用__next__()方法时返回一个字符串的对象——文件对象和列表对象都适用。

(Emphasis mine.) (强调我的。)

What you have is a wrapper around the file object.您拥有的是围绕文件对象的包装器 The file pointer does all the dirty work of efficiently iterating over the lines of your file, and the csv module's Reader parses those lines as they're read in.文件指针执行有效迭代文件行的所有脏工作,csv 模块的 Reader 在读入时解析这些行。

So yes, +1 for memory friendliness and efficiency.所以是的,+1 内存友好性和效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM