473,383 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Remove line breaks from a BIG text file

3
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)
Expand|Select|Wrap|Line Numbers
  1. f1 = open(fn1) 
  2. f2 = open(fn2, 'w') 
  3. outputlist = [line.strip() for line in f1] 
  4.  
  5. f2.write("\n".join(outputlist[:6]))
  6. f2.close() 
  7. f2 = open(fn2, 'a')
  8.  
  9. f2.write("\n")
  10. f2.write(" ".join(outputlist[6:]))
  11. f1.close()
  12. f2.close() 
  13.  
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.
Mar 8 '10 #1
2 2918
bvdet
2,851 Expert Mod 2GB
Instead of reading the entire file into memory, iterate on the file object and incrementally write to the output file.
Expand|Select|Wrap|Line Numbers
  1. f1 = open("test1.txt")
  2. f2 = open("test1_output.txt", "w")
  3. for i, line in enumerate(f1):
  4.     if i < 6:
  5.         f2.write(line)
  6.     else:
  7.         f2.write(line.strip())
  8. f1.close()
  9. f2.close()
Mar 8 '10 #2
Chris9
3
Thanks very much Bvdet. I had to add a line after line 7:
f2.write(" ")
Mar 9 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: lucas | last post by:
I have a text file that i would like to use PHP to read from. each line ends with a \n. i need to read from the beginning of the line, only upto that \n right now i'm using this, which is sloppy...
5
by: kevin arana | last post by:
Hello. Could anyone please show me an example of how to retrieve a specific line of a text file (say line 3 of data.txt) into a string variable? I have seen many examples on how to dump all...
1
by: vivekgrs | last post by:
hi iam reading text file thru vb 6.0 . now i want 2 count the no of lines in that text file. can anyone give me idea 2 retrieve the no of lines for reading , i had used the codes, Open sfil...
4
by: jeepers | last post by:
Hi all, my problem is delete a line in a text file. the file contain -n line with user, userCode. i want to replace the line with user is egual at user, and/or insert new line but delete the...
4
by: Johnymap | last post by:
Hi everyone. The text file i am using has the following format: "2001_1.rtf" (1 January 2001) Ookame v Tombale (CCF No. 203 of 2001) BWHC 1 "2001_2.rtf" (1 January 2001) Kgaimena v Leoifo...
5
by: Patrick David | last post by:
Hello NG, I am searching for a way to jump to a specific line in a text file, let's say to line no. 9000. Is there any method like file.seek() which leads me to a given line instead of a given...
5
by: kashif73 | last post by:
i have a text file which has a fixed length lines. How can I update a particular line (record ) basing on a record ID provided? below is my code which retrieves a particular line from the textfile &...
0
by: rahulsapra | last post by:
hello everyone m new to j2me plzz help I have a text file that contains data line by line.. format is : response:<some text><space>Pin:<some text><space>time:<hh:mm:ss> eg response:invalid...
4
by: Nawaf Ali | last post by:
I am trying to do some text statistics, like word frequency, average word length, average sentence length, and average paragraph length, I managed to do the word frequency and the average sentence...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.