944,198 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 28675
  • Python RSS
Jan 31st, 2007
0

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

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cornholio is offline Offline
4 posts
since Jan 2007
Jan 31st, 2007
0

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

Here is an example how to do this. I had to create your kind of data file first to use it properly ...
python Syntax (Toggle Plain Text)
  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()
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 5th, 2007
0

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

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()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cornholio is offline Offline
4 posts
since Jan 2007
Feb 5th, 2007
0

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

What is your error message?
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Feb 5th, 2007
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cornholio is offline Offline
4 posts
since Jan 2007
Feb 6th, 2007
0

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

Looks like you really butchered vegseat's code. Let me rewrite it a little:
Python Syntax (Toggle Plain Text)
  1. """ assume part of your list1.txt data file looks like this:
  2. 1846440556
  3. 1846440521
  4. 1846440491
  5. 1846440505
  6. 1846441137
  7. 1846441102
  8. 1846441080
  9. 1846441331
  10. 1846441323
  11. 1846441315
  12. """
  13.  
  14. # read the data file in as a list
  15. fin = open( 'list1.txt', "r" )
  16. data_list = fin.readlines()
  17. fin.close()
  18. # test first 5 list items ...
  19. print data_list[:5]
  20.  
  21. print '-'*60
  22.  
  23. # remove list items from index 3 to 5 (inclusive)
  24. del data_list[3:5+1]
  25. # test first 5 list items ...
  26. print data_list[:5]
  27.  
  28. # write the changed data (list) to a file
  29. fout = open("list2.txt", "w")
  30. fout.writelines(data_list)
  31. fout.close()
Last edited by vegaseat; Jan 7th, 2010 at 9:18 am. Reason: code tags
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Feb 7th, 2007
0

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

Big Thanks to all of you for help.

bumsfeld, your code does exactly what I wanted.

Cornholio - an absolute beginner
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cornholio is offline Offline
4 posts
since Jan 2007
Jan 7th, 2010
0
Re: how to remove a number of lines from a text file ?
When i try to use this code i still have the data in the orginial filem how do i remove them from the first file?
Reputation Points: 10
Solved Threads: 0
Light Poster
Godflesh is offline Offline
41 posts
since Nov 2009
Jan 7th, 2010
0
Re: how to remove a number of lines from a text file ?
Click to Expand / Collapse  Quote originally posted by Godflesh ...
When i try to use this code i still have the data in the orginial filem how do i remove them from the first file?
Replace
fout = open("list2.txt", "w")
with
fout = open("list1.txt", "w")
to overwrite the original file.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 7th, 2010
0
Re: how to remove a number of lines from a text file ?
thanks, offcourse
Reputation Points: 10
Solved Threads: 0
Light Poster
Godflesh is offline Offline
41 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: I've been trying to utilize Python import hooks.
Next Thread in Python Forum Timeline: Why doesnt this code work as it shout





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC