I need to remove line breaks from a large ASCII table, but get a MemoryError. The data is an ASCII grid: the header takes up the first 6 lines followed by 22000 rows of data. I want to remove the line breaks from the table part of the file while leaving the first 6 line breaks. The following code works for small files but runs out of memory (at the 3rd line) for large files: (thanks to Zhaphod and Bvdet from an earlier post)
I thought that if i could do the .strip() for chunks of f1 then i could append each to the external file, but couldn't work out how.
Code:
f1 = open(fn1)
f2 = open(fn2, 'w')
outputlist = [line.strip() for line in f1]
f2.write("\n".join(outputlist[:6]))
f2.close()
f2 = open(fn2, 'a')
f2.write("\n")
f2.write(" ".join(outputlist[6:]))
f1.close()
f2.close()
Comment