search a part in a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rommeladres
    New Member
    • Nov 2006
    • 4

    search a part in a string

    Completely newbie to scripting !! :
    I would like to get the subject out of a mail message and put this in a seperate string:
    mailMsg = mail.retr(i)[1]

    example result of mailMsg is :

    ['Return-Path: <Louisette.Snoe kx@123.com>', 'Delivered-To: 3-marc.jonkers@45 6.com', 'From: "louisette snoekx" <Louisette.Snoe kx@123.com>', 'To: <marc.jonkers@4 56.com>', 'References: <20060919215507 .cvi40klhf1q888 4k@webmail.456. com>', 'Subject: Gelezen: workshop 15/10 essen', 'Date: Sun, 5 Nov 2006 22:05:53 +0100', .. etc]

    I would like the string subject(i) to contain in this example 'Gelezen: workshop 15/10 essen'
    I suppose it must be something like

    for line in mailMsg:
    if line.find('Subj ect:')
    and then ???
  • fuffens
    New Member
    • Oct 2006
    • 38

    #2
    Code:
    mailMsg = ['Return-Path: <Louisette.Snoekx@123.com>', 'Delivered-To: 3-marc.jonkers@456.com', 'From: "louisette snoekx" <Louisette.Snoekx@123.com>', 'To: <marc.jonkers@456.com>', 'References: <20060919215507.cvi40klhf1q8884k@webmail.456.com>', 'Subject: Gelezen: workshop 15/10 essen', 'Date: Sun, 5 Nov 2006 22:05:53 +0100']
    subjectStr = 'Subject:'
    subject = []
    for item in mailMsg:
        if item.startswith(subjectStr):
            subject.append(item[len(subjectStr):].strip())
    print subject
    This will put the subject in a list assuming that mailMsg is a list of strings.

    BR
    /Fredrik

    Comment

    • rommeladres
      New Member
      • Nov 2006
      • 4

      #3
      Thanks a lot !
      This is aprox what I was looiking for!

      The result of the print is for example [' something']
      Is it possible to not showing the [' and the '] when printing ?

      Second question:
      if I do the same for the fromStr (mail comming from ...)
      the print result is in this example :
      "Louisette ,Snoekx "<Louisette.Sno ekx@123.com>
      Here I just want to show :Louisette.Snoe kx@123.com when printing
      so I only want what's between the < and >

      Once again thanks a lot for your help !

      Comment

      • kudos
        Recognized Expert New Member
        • Jul 2006
        • 127

        #4
        Maybe something like this (if I understood the question correctly)

        Code:
        s = "\"Louisette ,Snoekx \"<Louisette.Snoekx@123.com>"
        s =  ((((s.split("\""))[2]).split("<"))[1])
        print s[:len(s)-1]
        -kudos

        Originally posted by rommeladres
        Thanks a lot !
        This is aprox what I was looiking for!

        The result of the print is for example [' something']
        Is it possible to not showing the [' and the '] when printing ?

        Second question:
        if I do the same for the fromStr (mail comming from ...)
        the print result is in this example :
        "Louisette ,Snoekx "<Louisette.Sno ekx@123.com>
        Here I just want to show :Louisette.Snoe kx@123.com when printing
        so I only want what's between the < and >

        Once again thanks a lot for your help !

        Comment

        • rommeladres
          New Member
          • Nov 2006
          • 4

          #5
          Originally posted by kudos
          Maybe something like this (if I understood the question correctly)

          Code:
          s = "\"Louisette ,Snoekx \"<Louisette.Snoekx@123.com>"
          s =  ((((s.split("\""))[2]).split("<"))[1])
          print s[:len(s)-1]
          -kudos
          Kudos , thank you , but it doesn't seem to work.
          Is there another way to strip everything in front of and including < ?
          And is there another way to strip the last > ?

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by rommeladres
            Kudos , thank you , but it doesn't seem to work.
            Is there another way to strip everything in front of and including < ?
            And is there another way to strip the last > ?
            There IS another way (there is almost always another way in Python).
            Regular Expressions are what you need. Here is a link to a tutorial for using the re module:
            http://www.amk.ca/python/howto/regex/.
            Basically, (once you understand the syntax) you can tell a re.search() that you want everything that comes after "whatever" and is between "<" and ">" . Have fun and keep posting! Be sure to read the posting guidelines so that you know how to use code tags around the code in your posts. Thanks,
            Barton

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by rommeladres
              Thanks a lot !
              This is aprox what I was looiking for!

              The result of the print is for example [' something']
              Is it possible to not showing the [' and the '] when printing ?

              Second question:
              if I do the same for the fromStr (mail comming from ...)
              the print result is in this example :
              "Louisette ,Snoekx "<Louisette.Sno ekx@123.com>
              Here I just want to show :Louisette.Snoe kx@123.com when printing
              so I only want what's between the < and >

              Once again thanks a lot for your help !
              1)
              Code:
               
              mailMsg = ['Return-Path: <Louisette.Snoekx@123.com>', 'Delivered-To: 3-marc.jonkers@456.com', 'From: "louisette snoekx" <Louisette.Snoekx@123.com>', 'To: <marc.jonkers@456.com>', 'References: <20060919215507.cvi40klhf1q8884k@webmail.456.com>', 'Subject: Gelezen: workshop 15/10 essen', 'Date: Sun, 5 Nov 2006 22:05:53 +0100']
              subjectStr = 'Subject:'
              subject = []
              for item in mailMsg:
              	if item.startswith(subjectStr):
              		subject.append(item[len(subjectStr):].strip())
              for item in subject:
              	print item
              2)
              Code:
               
              fromaddress = ""
              for item in mailMsg: 
              	if item.startswith("From:"):
              		i = item.find("<") + 1
              		j = item.find(">", i)
              		fromaddress = item[i:j]
              print fromaddress
              As I said, there's (almost) always another way in python.

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                here's how to get help at the python prompt:

                Code:
                 
                >>> help(str.find)
                Help on method_descriptor: 
                 
                find(...)
                	S.find(sub [,start [,end]]) -> int
                 
                	Return the lowest index in S where substring sub is found,
                	such that sub is contained within s[start,end]. Optional
                	arguments start and end are interpreted as in slice notation.
                 
                	Return -1 on failure.

                Comment

                • rommeladres
                  New Member
                  • Nov 2006
                  • 4

                  #9
                  Thanks a lot bartonc !
                  Now it works !!

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    Originally posted by rommeladres
                    Thanks a lot bartonc !
                    Now it works !!
                    You are very welcome. Keep posting here on the python forum,
                    Barton

                    Comment

                    Working...