![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
Does anyone know how to remove a choosen line in a dat file?
i want to search for a word in the file, and then remove it if it exists, otherwise just return a message, saying not found? any ideas anyone? |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
Seems like there's del keyword in Python:
FileList = file("file.dat", "r").readlines()
del FileList[line-to-delete]
file("file.dat", "w").writelines(FileList)Last edited by EverLearning; Jun 9th, 2005 at 4:38 PM. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
How do i find the line number returned from the searched word then?? lol
thanks for that |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
To return line# where the word is:
look up .filelineno() and .find() Last edited by EverLearning; Jun 9th, 2005 at 6:01 PM. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
Ok, it was bugging me, so i did it over the break. Here's my vesion of "find a line where the search_word is" and it works:
#!usr/bin/env python
import sys, fileinput
for line in fileinput.input():
if line.find("search_word") >= 0:
print "line is %d" % (fileinput.filelineno())$ python script.py text.dat Last edited by EverLearning; Jun 9th, 2005 at 8:05 PM. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
Don't Work
¬_¬ hehe, pulls up a nice attribute error ![]() |
|
|
|
|
|
#7 | |
|
Newbie
|
Quote:
look up how to use the .find() function and try to make the code yourself (using the method in the first example should help) |
|
|
|
|
|
|
#8 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
don't worry sorted it.
are your indentions meant to be so far out?? mine works and they are set as there meant to!! import sys, fileinput
search = raw_input("Search: ")
for line in fileinput.input("text.txt"):
if line.find(search) >= 0:
print "line is %d" % (fileinput.filelineno()) |
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#10 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
that was all 2 tabs..
i have sorted it all anyway |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|