| | |
how to remove a number of lines from a text file ?
Thread Solved |
•
•
Join Date: Jan 2007
Posts: 4
Reputation:
Solved Threads: 0
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
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
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)
data_str = """\ 1846440556 1846440521 1846440491 1846440505 1846441137 1846441102 1846441080 1846441331 1846441323 1846441315""" # let's create your data file from the string 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:5+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()
May 'the Google' be with you!
•
•
Join Date: Jan 2007
Posts: 4
Reputation:
Solved Threads: 0
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()
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()
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]
[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]
![]() |
Similar Threads
- Need Help With Simple 'c' Program!!! (C)
- turning userinput into a text file (C)
- Homework: filling array from text file (VB.NET)
- Help Reading Info in Text File Into an Array (C++)
- 10 line text file (Java)
Other Threads in the Python Forum
- Previous Thread: Opening a *.exe with python script?
- Next Thread: [python] bulding generic data type
| Thread Tools | Search this Thread |
alarm ansi assignment avogadro backend beginner binary bluetooth character cipher cmd customdialog cx-freeze data decimals dictionary directory dynamic error exe file float format function generator getvalue gnu graphics halp heads homework http ideas import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path pointer port prime programming progressbar push py2exe pygame python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh statistics string strings sudokusolver sum text thread threading time tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia write wxpython xlib






