2

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
5
  • 2
    How long are the lines?! Commented Oct 15, 2015 at 14:51
  • Maybe use file.read(size_in_bytes) to read the file (and super-long lines) in chunks? Commented Oct 15, 2015 at 14:56
  • Lines should be 200 characters long maximum. Smaller files are correctly opened with Notepad++ and Excel. Commented Oct 15, 2015 at 14:59
  • I'm working in Windows 7, file was created in other Python script with no problem, with lineterminator = '\r' Commented Oct 15, 2015 at 15:01
  • 2
    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. Commented Oct 15, 2015 at 15:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Isn't there a way to tell Python to interpret \r as newline?
@José Not sure, I think there should be, but I could not find such a parameter on open or file.read.
@José I think you could use a csv.reader and configure an according dialect. Even if the file is not CSV, you could read it as a CSV file with just one column.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.