how to remove a number of lines from a text file ?

Thread Solved

Join Date: Jan 2007
Posts: 4
Reputation: Cornholio is an unknown quantity at this point 
Solved Threads: 0
Cornholio Cornholio is offline Offline
Newbie Poster

how to remove a number of lines from a text file ?

 
0
  #1
Jan 31st, 2007
Hi,

I have jest started learning Python.

I would like to get some help on writing a script that would delete a set number of lines from a text file which looks like this :

1846440556
1846440521
1846440491
1846440505
1846441137
1846441102
1846441080
1846441331
1846441323
1846441315
...
...

Thanks in advance

Cornholio
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: how to remove a number of lines from a text file ?

 
0
  #2
Jan 31st, 2007
Here is an example how to do this. I had to create your kind of data file first to use it properly ...
  1. data_str = """\
  2. 1846440556
  3. 1846440521
  4. 1846440491
  5. 1846440505
  6. 1846441137
  7. 1846441102
  8. 1846441080
  9. 1846441331
  10. 1846441323
  11. 1846441315"""
  12.  
  13. # let's create your data file from the string
  14. fout = open("MyData1.txt", "w")
  15. fout.write(data_str)
  16. fout.close()
  17.  
  18. # read the data file in as a list
  19. fin = open( 'MyData1.txt', "r" )
  20. data_list = fin.readlines()
  21. fin.close()
  22. # test it ...
  23. print data_list
  24.  
  25. print '-'*60
  26.  
  27. # remove list items from index 3 to 5 (inclusive)
  28. del data_list[3:5+1]
  29. # test it ...
  30. print data_list
  31.  
  32. # write the changed data (list) to a file
  33. fout = open("MyData2.txt", "w")
  34. fout.writelines(data_list)
  35. fout.close()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 4
Reputation: Cornholio is an unknown quantity at this point 
Solved Threads: 0
Cornholio Cornholio is offline Offline
Newbie Poster

Re: how to remove a number of lines from a text file ?

 
0
  #3
Feb 5th, 2007
Thanks fot your help.
I have now modified the script to read the lines from a file. The Python interpreter gives me an error message. What is wrong with the modified code ?
data1 = open('list.txt', "r")
data_str = data1.readlines()
data1.close()
# create your data file from the string
# try 'wb'?
fout = open("MyData1.txt", "w")
fout.write(data_str)
fout.close()

# read the data file in as a list
fin = open( 'MyData1.txt', "r" )
data_list = fin.readlines()
fin.close()
# test it ...
print data_list

print '-'*60

# remove list items from index 3 to 5 (inclusive)
del data_list[3:200+1]
# test it ...
print data_list

# write the changed data (list) to a file
fout = open("MyData2.txt", "w")
fout.writelines(data_list)
fout.close()
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: how to remove a number of lines from a text file ?

 
0
  #4
Feb 5th, 2007
What is your error message?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 4
Reputation: Cornholio is an unknown quantity at this point 
Solved Threads: 0
Cornholio Cornholio is offline Offline
Newbie Poster

Re: how to remove a number of lines from a text file ?

 
0
  #5
Feb 5th, 2007
the error message is:

File "C:\!cutout\cutOut.py", line 6, in <module>
fout.write(data_str)
TypeError: argument 1 must be string or read-only character buffer, not list
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: how to remove a number of lines from a text file ?

 
0
  #6
Feb 6th, 2007
Looks like you really butchered vegseat's code. Let me rewrite it a little:
[php]""" assume part of your list1.txt data file looks like this:
1846440556
1846440521
1846440491
1846440505
1846441137
1846441102
1846441080
1846441331
1846441323
1846441315
"""

# read the data file in as a list
fin = open( 'list1.txt', "r" )
data_list = fin.readlines()
fin.close()
# test first 5 list items ...
print data_list[:5]

print '-'*60

# remove list items from index 3 to 5 (inclusive)
del data_list[3:5+1]
# test first 5 list items ...
print data_list[:5]

# write the changed data (list) to a file
fout = open("list2.txt", "w")
fout.writelines(data_list)
fout.close()
[/php]
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 4
Reputation: Cornholio is an unknown quantity at this point 
Solved Threads: 0
Cornholio Cornholio is offline Offline
Newbie Poster

Re: how to remove a number of lines from a text file ?

 
0
  #7
Feb 7th, 2007
Big Thanks to all of you for help.

bumsfeld, your code does exactly what I wanted.

Cornholio - an absolute beginner
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC