473,396 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

simple string search and replace

Kun
hey guys, here's my code,

senders = [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
<an**@anon.wharton.com>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS
(FROM)] {37}', 'From: Kun <ne*******@gmail.com>\r\n\r\n'), ')']
print senders
parsed_senders = []
sender = ""
for item in senders:
if isinstance(item,tuple):
item= ''.join(item)
if item==')':
parsed_senders.append(sender[sender.find('<')+1:].strip())
sender = ""
else:
sender+=item
print parsed_senders


wondering if anyone knows how i can remove the '>'s from the list, which
outputs to something like ['an**@anon.wharton.com>', 'ne*******@gmail.com>']
Mar 25 '06 #1
2 1124
Generally, to remove a substring (like ">") from a string you can use
the replace method (that returns a new string):
s = "...anon.wharton.com>..."
s.replace(">", "")

'...anon.wharton.com...'

You can use it with something like:
print [s.replace(">", "") for s in parsed_senders]

or you can put the replace() somewhere in the main loop.

Probably to solve your problem there are other solutions, like using a
RE to find email addresses inside the string...

Bye,
bearophile

Mar 25 '06 #2
Kun wrote:
hey guys, here's my code,

senders = [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
<an**@anon.wharton.com>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS
(FROM)] {37}', 'From: Kun <ne*******@gmail.com>\r\n\r\n'), ')']
print senders
parsed_senders = []
sender = ""
for item in senders:
if isinstance(item,tuple):
item= ''.join(item)
if item==')':
parsed_senders.append(sender[sender.find('<')+1:].strip())
sender = ""
else:
sender+=item
print parsed_senders


wondering if anyone knows how i can remove the '>'s from the list, which
outputs to something like ['an**@anon.wharton.com>', 'ne*******@gmail.com>']


Where you append to parsed_senders, replace

sender[sender.find('<')+1:]

with

sender[sender.find('<')+1:-1]

and that will use a string one shorter, omitting the ">" character.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

Mar 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: C L Humphreys | last post by:
Hi, I'm used to programming with Java, and am unsure of how this may work within VB. I want to pass some strings to a function, and alter them within the function. I want the strings to stay...
2
by: gyromagnetic | last post by:
Hi, I have written a function that searches a text string for various words. The text is searched using a boolean 'and' or a boolean 'or' of the input list of search terms. Since I need to use...
4
by: beliavsky | last post by:
The code for text in open("file.txt","r"): print text.replace("foo","bar") replaces 'foo' with 'bar' in a file, but how do I avoid changing text inside single or double quotes? For making...
1
by: timothy ma and constance lee | last post by:
Sir I have the simple question as follows: I got three pages a,b,c point to same page d when clicking submit. Is there any way, by using JavaScript to trace back which page a or b or c to...
4
by: suzy | last post by:
hello, i want to search a text file for an occurrence of a string and replace some text after the match, then save the file. eg: if the file is: <start> Hello my name is tony, I am 22....
2
by: Evan | last post by:
Hey, I posted this yesterday, but no one had any ideas? C'mon now, I know this isn't that hard, i'm just a little new to javascript, and I can't quite figure this out. I searched and searched to...
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
1
by: DougJrs | last post by:
Good Morning, I am trying to write a simple function that would grab the "errorId" parameter and then display a message when the page loads. I basically have a login page (login.asp) that if...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
5
by: buu | last post by:
I have an function that replaces some string from a huge text that I run very often... So, I wanted to speed it up... I was using String and StringBuilder. But, I was wandering should same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.