LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-24-2009, 10:43 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
How to remove lines and parts of lines from python strings?


How do you remove parts of strings using python? Such as, if I have something like:
Code:
erme1 sdifskenklsd
erme2 sdfjksliel
erme3 kcielsmkdlfdsiof
erme4 mvieksldiskld
erme5 cidfksldosldk
How could I have python remove the erme# in front of each row? Is there a way to do it by removing the erme plus what ever is attached to it? Or just remove the first 5 characters of each row? Or is there an option for each? I'd like to know how to do both, but I'd be happy with just one.

Another case, how would I have python remove something like:
Code:
4:32 aips mana seven
dfkjdsfjlfdsjkfdskjlfdskjldsf
fdsnhjfdsnfgewonkclewnloew
fdsjewofenwjlkfewniofewnkilew
dsnkjlfewniofewnjkldsienklew
4:32 aips mana seven
fdsjklfdsjfeiodfsnkvdslkdsl
sfdnklfdsknlfdsnlkfdlkds
fdsnklfdslkfdslkfdslkfdslkdfs
fdsnklfdsklndsnlkfdsnkldsnkdl
4:33 aips mana seven
fdsjkdsfkljdsfkjdsopejwkl
wejiofejlfdemskfdsjmkflds
4:33 aips mana seven
fdsjfdsfdslkfjkldsfjkfdsjk
4:34 aips mana seven
How could I remove the entire line starting with the time leaving the rest, when the same time could be repeated any number of times before moving to the next time?

Thanks for your time!
 
Old 11-24-2009, 11:39 PM   #2
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Hello !

You (probably) need to use regular expressions:

http://docs.python.org/library/re.html

Just to get you started:

Code:
>>> re.sub('^erme[0-9]*\s', '', 'erme3 kcielsmkdlfdsiof')
'kcielsmkdlfdsiof'
>>>
Of course I do have to warn you I don't python every day so there may be better ways of doing this.

Cheers
 
Old 11-25-2009, 01:46 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
@OP, you probably should read up the docs for a start.

for you first question and with your sample data, you can just do a split(), then remove the first column
Code:
for line in open("file"):
    print ' '.join(line.split()[1:])
to remove first 5 characters
Code:
print line[4:]
these are just the basics. so read up the docs, then try to do the third problem. Regex expression?? NO NEED.
 
Old 11-26-2009, 11:29 PM   #4
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Great thanks. I think I will read more documentation.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract lines containing some strings without affectting sequential order cgcamal Programming 7 11-06-2008 11:57 PM
Awk Question to search specific strings grouped by blank lines rk4k Programming 6 07-07-2008 11:56 PM
Asterisk box to connect VoIP lines with normal telephone lines. sraju Linux - Software 1 11-08-2006 12:38 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM


All times are GMT -5. The time now is 04:48 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration