Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 9th, 2005, 2:51 PM   #1
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
Unhappy Remove line from dat file

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?
SaturN is offline   Reply With Quote
Old Jun 9th, 2005, 3:33 PM   #2
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
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)
line-to-delete is the number of the line in file or list [0-n].
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics

Last edited by EverLearning; Jun 9th, 2005 at 3:38 PM.
EverLearning is offline   Reply With Quote
Old Jun 9th, 2005, 3:42 PM   #3
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
How do i find the line number returned from the searched word then?? lol
thanks for that
SaturN is offline   Reply With Quote
Old Jun 9th, 2005, 4:27 PM   #4
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
To return line# where the word is:
look up .filelineno() and .find()
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics

Last edited by EverLearning; Jun 9th, 2005 at 5:01 PM.
EverLearning is offline   Reply With Quote
Old Jun 9th, 2005, 7:03 PM   #5
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
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())
call it as
$ python script.py text.dat
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics

Last edited by EverLearning; Jun 9th, 2005 at 7:05 PM.
EverLearning is offline   Reply With Quote
Old Jun 10th, 2005, 1:51 PM   #6
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
Don't Work
¬_¬
hehe, pulls up a nice attribute error
SaturN is offline   Reply With Quote
Old Jun 10th, 2005, 2:08 PM   #7
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
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())
SaturN is offline   Reply With Quote
Old Jun 10th, 2005, 2:10 PM   #8
CrAzY_J
Newbie
 
Join Date: May 2005
Posts: 29
Rep Power: 0 CrAzY_J is on a distinguished road
Send a message via MSN to CrAzY_J
Quote:
Originally Posted by SaturN
Don't Work
¬_¬
hehe, pulls up a nice attribute error
Well he basicly already gave you enough information to do this on your own.

look up how to use the .find() function and try to make the code yourself (using the method in the first example should help)
CrAzY_J is offline   Reply With Quote
Old Jun 10th, 2005, 2:20 PM   #9
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
Quote:
Originally Posted by SaturN
don't worry sorted it.
are your indentions meant to be so far out?? mine works and they are set as there meant to!!
Yes, it has to be a tab, at least for my interpreter.
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Jun 10th, 2005, 2:37 PM   #10
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
that was all 2 tabs..
i have sorted it all anyway
SaturN is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:44 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC