José
José

Reputation: 1720

Python 2.5 MemoryError with large file

I can't read a big file because of MemoryError. Other answers suggest this should work, what may be the problem?
The whole code is this

#Separador de RAMAS.csv por rama
import csv
archivo_original = open('RAMAS.csv')
print archivo_original.readline()

And the error I get is this

Traceback (most recent call last):
  File "Separador_CSV.py", line 4, in <module>
    print archivo_original.readline()
MemoryError

Upvotes: 2

Views: 102

Answers (2)

tobias_k
tobias_k

Reputation: 82889

I'm working in Windows 7, file was created in other Python script with no problem, with lineterminator = '\r'

file.readline does not seem to split on \r. If you created that file with another Python script, better create it again with \n instead of \r, then it should work.

Example with \r:

In [14]: open("test", "w").write("foo\rbar\rblub")
In [15]: open("test", "r").readline()
Out[15]: 'foo\rbar\rblub'

Example with \n:

In [16]: open("test", "w").write("foo\nbar\nblub")
In [17]: open("test", "r").readline()
Out[17]: 'foo\n'

If that's not possible, you can use file.read(size_in_bytes) to read just a chunk of the file and re-create the lines yourself.

Upvotes: 1

Jos&#233;
Jos&#233;

Reputation: 1720

I've found Python interprets the file as being one big line.
Thanks for the comments, made me find the problem.

Upvotes: 0

Related Questions