Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 24, 2010, 01:14 PM   #1
Nsutton
macrumors member
 
Join Date: Dec 2009
Location: 6 Feet Under
Python removing lines from a text file

ok so, I want to have the bottom code to search that txt file find the line that contains the search criteria and then delete the line that contains the search criteria.



Ideas?


Code:
if action2=='2':
		fob=open('/Users/NSutton/Simple Adress/bookdata.txt','r')
		searchdelete=raw_input("Enter the name of the contact you'd like to delete: ")
		for line in fob:
			if searchdelete in line:
    				print line
__________________
An Apple a day keeps viruses away!

Last edited by Nsutton; Apr 24, 2010 at 02:17 PM.
Nsutton is offline   0 Reply With Quote
Old Apr 24, 2010, 01:41 PM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Instead of printing any line containing the search criteria, copy (append) any line that does not contain the search criteria to a destination file, or to memory. Essentially, you want to copy your source file line by line to a destination file, but skip any line where you find a match. If you want it to replace your source file, make the destination file a temp file, and when done, delete the source file and rename the temp file to the source file's name.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Apr 24, 2010, 02:06 PM   #3
Nsutton
Thread Starter
macrumors member
 
Join Date: Dec 2009
Location: 6 Feet Under
Quote:
Originally Posted by HiRez View Post
Instead of printing any line containing the search criteria, copy (append) any line that does not contain the search criteria to a destination file, or to memory. Essentially, you want to copy your source file line by line to a destination file, but skip any line where you find a match. If you want it to replace your source file, make the destination file a temp file, and when done, delete the source file and rename the temp file to the source file's name.
I understand in concept but am lost in actually putting that to code....
__________________
An Apple a day keeps viruses away!
Nsutton is offline   0 Reply With Quote
Old Apr 24, 2010, 02:10 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
It seems like changing your if to say not in line, that would do it.

-Lee
lee1210 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Three Lines Of Text For Filenames In Finder steiney Mac Applications and Mac App Store 16 Aug 15, 2014 01:22 PM
TxtPub - Text file reader app by converting text to ePub automatically. Ralph Kim iPad Apps 0 Apr 26, 2013 09:13 PM
Sorting lines of text, not alphabetically, but buy value ($$)... madsylar Mac Programming 4 Apr 8, 2013 07:05 AM
Python IDE or text editor that can recognize API specific syntax thekev Mac Programming 2 Sep 4, 2012 05:49 AM
TextEdit - removing lines in a box barola Mac Basics and Help 0 Jun 20, 2012 06:37 AM

Forum Jump

All times are GMT -5. The time now is 05:33 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC