473,791 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple string search and replace

Kun
hey guys, here's my code,

senders = [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
<an**@anon.whar ton.com>\r\n\r\ n'), ')', ('462 (BODY[HEADER.FIELDS
(FROM)] {37}', 'From: Kun <ne*******@gmai l.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.whar ton.com>', 'ne*******@gmai l.com>']
Mar 25 '06 #1
2 1143
Generally, to remove a substring (like ">") from a string you can use
the replace method (that returns a new string):
s = "...anon.wharto n.com>..."
s.replace(">", "")

'...anon.wharto n.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.whar ton.com>\r\n\r\ n'), ')', ('462 (BODY[HEADER.FIELDS
(FROM)] {37}', 'From: Kun <ne*******@gmai l.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.whar ton.com>', 'ne*******@gmai l.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.blogs pot.com

Mar 25 '06 #3

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

Similar topics

9
2405
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 altered when the function ends *without returning the strings explicitly*. I think this is to do with passing references? How should it work (if possible?) using VB?
2
2400
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 this function for many long strings and many search words, I would like to use as efficient a method as possible. Are there improvements that can be made to the code below? Are there better alternatives?
4
3680
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 changes to Python code, I would also like to avoid changing text in comments, either the '#' or '""" ... """' kind.
1
1258
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 reach it, once exit button is clicked on page d? thanks
4
2211
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. Hello, my name is sonia, I am 18.
2
8376
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 try and find the answer to this, but I had no luck anywhere. It's a little different situation than I found anywhere else. I would simply put javascript into the <a> tag, but like I said, It's delivered via PHP, and I don't have access to it. I...
21
3413
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 brackets are not regular expression syntax. Thanks,
1
1582
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 the login fails the user is directed page to the login page with an errorId parameter (login.asp? errorId=1). I wrote the javascript below to grab the paramater and then retuen a message (I borrowed the gup function from an example, and wrote the
3
3965
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 below and it works fine as long as I'm not adding tags around the to parameter. Can anyone explain to me why it doesn't work with tags? And it needs to be XSLT 1.0. This works: X<xsl:value-of select="'little steak'"/>X This doesn't work:...
5
2320
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 function in c++ be faster? I tried to test it and it wasn't... why? Is there any example of faster functions in VC than in VB? (using c++ or c)
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9995
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.