how to append semicolon to a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yaffa

    #1

    how to append semicolon to a variable

    dear folks,

    i have the following lines of python code:

    couch = incident.findNe xtSibling('td')
    price = couch.findNextS ibling('td')
    sdate = price.findNextS ibling('td')
    city = sdate.findNextS ibling('td')
    strUrl = addr.b.string
    currently what this ends up doing is creating something like this

    couch3201/01/2004newyork

    now what i want to do is add a semicolon after the couch, price, sdate,
    city so that i get something like this

    couch;32;01/01/2004;new york

    does anyone know how to do this?

    thanks

    yaffa

    p.s. i tried couch = couch + ';' and then i tried couch = couch + ";"
    and then i tried couch = couch.append ';'

  • Grant Edwards

    #2
    Re: how to append semicolon to a variable

    On 2005-08-13, yaffa <yaffalandis@gm ail.com> wrote:
    [color=blue]
    > i have the following lines of python code:
    >
    > couch = incident.findNe xtSibling('td')
    > price = couch.findNextS ibling('td')
    > sdate = price.findNextS ibling('td')
    > city = sdate.findNextS ibling('td')
    > strUrl = addr.b.string
    > currently what this ends up doing is creating something like this
    >
    > couch3201/01/2004newyork
    >
    > now what i want to do is add a semicolon after the couch, price, sdate,
    > city so that i get something like this
    >
    > couch;32;01/01/2004;new york[/color]

    Try this:

    s = ';'.join([couch,price,sda te,city])
    print s
    [color=blue]
    > p.s. i tried couch = couch + ';'
    > and then i tried couch = couch + ";"[/color]

    both of those should have worked fine.
    [color=blue]
    > and then i tried couch = couch.append ';'[/color]

    1) The append() method of a sequence doesn't return anything.

    2) You call methods using ()

    3) String are immutable, and therefore don't have an append
    method.

    Perhaps you ought to read through the tutorial?

    --
    Grant Edwards grante Yow! LOOK!!! I'm WALKING
    at in my SLEEP again!!
    visi.com

    Comment

    • Bengt Richter

      #3
      Re: how to append semicolon to a variable

      On 13 Aug 2005 08:14:32 -0700, "yaffa" <yaffalandis@gm ail.com> wrote:
      [color=blue]
      >dear folks,
      >
      >i have the following lines of python code:
      >
      > couch = incident.findNe xtSibling('td')
      > price = couch.findNextS ibling('td')
      > sdate = price.findNextS ibling('td')
      > city = sdate.findNextS ibling('td')
      > strUrl = addr.b.string
      >currently what this ends up doing is creating something like this
      >
      >couch3201/01/2004newyork
      >
      >now what i want to do is add a semicolon after the couch, price, sdate,
      >city so that i get something like this
      >
      >couch;32;01/01/2004;new york
      >
      >does anyone know how to do this?
      >
      >thanks
      >
      >yaffa
      >
      >p.s. i tried couch = couch + ';' and then i tried couch = couch + ";"
      >and then i tried couch = couch.append ';'
      >[/color]
      Assuming all your findNextSibling calls result in strings, as in
      [color=blue][color=green][color=darkred]
      >>> couch = 'couch'
      >>> price = '32'
      >>> sdate = '01/01/2004'
      >>> city = 'newyork'
      >>>[/color][/color][/color]

      You can put them in a list[color=blue][color=green][color=darkred]
      >>> [couch, price, sdate, city][/color][/color][/color]
      ['couch', '32', '01/01/2004', 'newyork']

      And join them with a string inserted between each successive pair of elements
      [color=blue][color=green][color=darkred]
      >>> ';'.join([couch, price, sdate, city])[/color][/color][/color]
      'couch;32;01/01/2004;newyork'

      If you like a space after each ';', just include it in the joiner string
      [color=blue][color=green][color=darkred]
      >>> '; '.join([couch, price, sdate, city])[/color][/color][/color]
      'couch; 32; 01/01/2004; newyork'[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      If all your items weren't string type, you'll have to convert first, e.g.,[color=blue][color=green][color=darkred]
      >>> price = 32
      >>> '; '.join([couch, price, sdate, city])[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      TypeError: sequence item 1: expected string, int found[color=blue][color=green][color=darkred]
      >>> '; '.join(map(str,[couch, price, sdate, city]))[/color][/color][/color]
      'couch; 32; 01/01/2004; newyork'

      If you have 2.4 and don't like map, you can use a generator expression:
      [color=blue][color=green][color=darkred]
      >>> '; '.join(str(x) for x in (couch, price, sdate, city))[/color][/color][/color]
      'couch; 32; 01/01/2004; newyork'

      or if you want the quoted strings
      [color=blue][color=green][color=darkred]
      >>> '; '.join(repr(x) for x in (couch, price, sdate, city))[/color][/color][/color]
      "'couch'; 32; '01/01/2004'; 'newyork'"

      Note: if you want ';' appended to each item instead of just between items, you
      can just add ';' to the whole thing
      [color=blue][color=green][color=darkred]
      >>> '; '.join(str(x) for x in (couch, price, sdate, city)) + ';'[/color][/color][/color]
      'couch; 32; 01/01/2004; newyork;'

      Which avoids the trailing blank you would get if you appended '; ' to every item
      before joining them, as in
      [color=blue][color=green][color=darkred]
      >>> ''.join(('%s; '%x) for x in (couch, price, sdate, city))[/color][/color][/color]
      'couch; 32; 01/01/2004; newyork; '

      or
      [color=blue][color=green][color=darkred]
      >>> ''.join((str(x) + '; ') for x in (couch, price, sdate, city))[/color][/color][/color]
      'couch; 32; 01/01/2004; newyork; '

      HTH

      Regards,
      Bengt Richter

      Comment

      • tiissa

        #4
        Re: how to append semicolon to a variable

        Grant Edwards wrote:[color=blue]
        > On 2005-08-13, yaffa <yaffalandis@gm ail.com> wrote:
        >[color=green]
        >>i have the following lines of python code:
        >>
        >> couch = incident.findNe xtSibling('td')
        >> price = couch.findNextS ibling('td')
        >> sdate = price.findNextS ibling('td')
        >> city = sdate.findNextS ibling('td')
        >> strUrl = addr.b.string
        >>currently what this ends up doing is creating something like this
        >>
        >>couch3201/01/2004newyork
        >>
        >>now what i want to do is add a semicolon after the couch, price, sdate,
        >>city so that i get something like this
        >>
        >>couch;32;01/01/2004;new york[/color]
        >
        >
        > Try this:
        >
        > s = ';'.join([couch,price,sda te,city])
        > print s[/color]

        I'll risk myself with something like:

        s = ';'.join([tag.string for tag in [couch,price,sda te,city]])

        Of course, from the question I wouldn't have any clue. I just like doing
        some guessing on problems I know nothing about. ;)
        [color=blue][color=green]
        >>p.s. i tried couch = couch + ';'
        >>and then i tried couch = couch + ";"[/color]
        >
        > both of those should have worked fine.[/color]

        Not really. It seems to me the OP is using BeautifulSoup (or some other
        SGML parser). In this case, couch and others are not strings but objects.
        It may also be that strUrl is their parent (but I wouldn't know, how
        would I?)
        [color=blue]
        > Perhaps you ought to read through the tutorial?[/color]

        That's always useful.
        However, the first thing is to put the minimal context in your question
        to help the people you want your answers from understanding your issue.
        I would advise you to read tutorials and documentations on the modules
        you're using as well as learning to ask meaningful questions[1].


        [1] http://www.catb.org/~esr/faqs/smart-questions.html

        Comment

        • Grant Edwards

          #5
          Re: how to append semicolon to a variable

          On 2005-08-13, tiissa <tiissa@nonfree .fr> wrote:[color=blue]
          > Grant Edwards wrote:[/color]
          [color=blue][color=green]
          >> s = ';'.join([couch,price,sda te,city])
          >> print s[/color]
          >
          > I'll risk myself with something like:
          >
          > s = ';'.join([tag.string for tag in [couch,price,sda te,city]])
          >
          > Of course, from the question I wouldn't have any clue. I just like doing
          > some guessing on problems I know nothing about. ;)
          >[color=green][color=darkred]
          >>>p.s. i tried couch = couch + ';'
          >>>and then i tried couch = couch + ";"[/color]
          >>
          >> both of those should have worked fine.[/color]
          >
          > Not really. It seems to me the OP is using BeautifulSoup (or some other
          > SGML parser). In this case, couch and others are not strings but objects.[/color]

          Seems like a reasonable guess.
          [color=blue]
          > It may also be that strUrl is their parent (but I wouldn't know, how
          > would I?)[/color]

          Nope. :)

          --
          Grant Edwards grante Yow! Are you mentally here
          at at Pizza Hut??
          visi.com

          Comment

          Working...