-1

I am reading the file using readlines() function in python and storing the data to variable. 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?

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

Where Line is the List

2 Answers2

0

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)

Mohammad Jafari
  • 1,742
  • 13
  • 17
0

yeild is one of the best approach to handle huge data. Following link will helpful for you

https://stackoverflow.com/questions/519633/lazy-method-for-reading-big-file-in-python
Saisiva A
  • 595
  • 6
  • 24