简体   繁体   English

获取内存错误以使用 readline python 执行操作

[英]Getting Memory error to perform operation with readline python

I am reading the file using readlines() function in python and storing the data to variable.我正在使用 python 中的 readlines() 函数读取文件并将数据存储到变量中。 it is worked fine with small text file, but getting the "Memory error" for big sized file.它适用于小文本文件,但对于大文件会出现“内存错误”。

Is there something we can use instead of readline() and store the data to varibale ass list?有什么我们可以使用而不是 readline() 并将数据存储到可变屁股列表的东西吗?

with open("some_test.txt") as fp:
    line= fp.readlines()

Where Line is the List

You can iterate over lines您可以遍历行

Try this way:试试这个方法:

with open("some_test.txt") as fp:
    for line in fp:
        do_your_stuff(line)
        ...

EDITED: I made a mistake (didn't iterate)编辑:我犯了一个错误(没有迭代)

yeild is one of the best approach to handle huge data. yeild 是处理海量数据的最佳方法之一。 Following link will helpful for you以下链接对您有帮助

https://stackoverflow.com/questions/519633/lazy-method-for-reading-big-file-in-python

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

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