Splitting device addresses into parts

Discussion in 'Python' started by Fabian Steiner, Sep 26, 2006.

  1. I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
    need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
    simple way to achieve this? So far I am using regular expressions but I
    would like to avoid them ...

    Regards,
    Fabian Steiner
    Fabian Steiner, Sep 26, 2006
    #1
    1. Advertising

  2. Fabian Steiner wrote:
    > I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
    > need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
    > simple way to achieve this? So far I am using regular expressions but I
    > would like to avoid them ...


    devices = ["PCI:2:3.0", "PCI:3.4:0"]
    for d in device:
    nums = tuple(map(int, d.split(':')[1:]))
    print "for ", d, " : ", nums


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    Bruno Desthuilliers, Sep 26, 2006
    #2
    1. Advertising

  3. Bruno Desthuilliers wrote:
    > Fabian Steiner wrote:
    >> I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
    >> need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
    >> simple way to achieve this? So far I am using regular expressions but I
    >> would like to avoid them ...

    >
    > devices = ["PCI:2:3.0", "PCI:3.4:0"]
    > for d in device:
    > nums = tuple(map(int, d.split(':')[1:]))
    > print "for ", d, " : ", nums


    Unfortunately, this doesn't work (even if I correct your typos) since
    the delimeter isn't necessary a colon - that's exactly the difficulty I
    am trying to solve.

    Regards,
    Fabian Steiner
    Fabian Steiner, Sep 26, 2006
    #3
  4. Fabian Steiner

    Guest

    This may be a rare case where regular expressions are not a horrible,
    self-defeating idea. Something like:

    delimiter = re.compile("[:\.]")
    delimiter.split("PCI:2:3.0")
    ....and then ignore the first entry, and map int the rest.
    Alternatively, if the delimiters can really be anything, and if there
    are no numbers in the first space ("PCI"), then maybe this approach:

    number = re.compile("\d+?")
    number.findall("PCI:2:3.0")

    Fabian Steiner wrote:
    > Bruno Desthuilliers wrote:
    > > Fabian Steiner wrote:
    > >> I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
    > >> need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
    > >> simple way to achieve this? So far I am using regular expressions but I
    > >> would like to avoid them ...

    > >
    > > devices = ["PCI:2:3.0", "PCI:3.4:0"]
    > > for d in device:
    > > nums = tuple(map(int, d.split(':')[1:]))
    > > print "for ", d, " : ", nums

    >
    > Unfortunately, this doesn't work (even if I correct your typos) since
    > the delimeter isn't necessary a colon - that's exactly the difficulty I
    > am trying to solve.
    >
    > Regards,
    > Fabian Steiner
    , Sep 26, 2006
    #4
  5. Fabian Steiner

    Paddy Guest

    Fabian Steiner wrote:
    > Bruno Desthuilliers wrote:
    > > Fabian Steiner wrote:
    > >> I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
    > >> need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
    > >> simple way to achieve this? So far I am using regular expressions but I
    > >> would like to avoid them ...

    > >
    > > devices = ["PCI:2:3.0", "PCI:3.4:0"]
    > > for d in device:
    > > nums = tuple(map(int, d.split(':')[1:]))
    > > print "for ", d, " : ", nums

    >
    > Unfortunately, this doesn't work (even if I correct your typos) since
    > the delimeter isn't necessary a colon - that's exactly the difficulty I
    > am trying to solve.
    >
    > Regards,
    > Fabian Steiner

    Fabian,
    You should have given better examples, but, on what you have told us so
    far...

    for ch in delimeter_chars:
    s.replace(ch, some_char_not_in_input)
    tple = tuple(int(x) for x in s.split(some_char_not_in_input)[1:])

    - Pad.
    Paddy, Sep 26, 2006
    #5
  6. Fabian Steiner

    Steve Holden Guest

    Fabian Steiner wrote:
    > Bruno Desthuilliers wrote:
    >
    >>Fabian Steiner wrote:
    >>
    >>>I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
    >>>need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
    >>>simple way to achieve this? So far I am using regular expressions but I
    >>>would like to avoid them ...

    >>
    >>devices = ["PCI:2:3.0", "PCI:3.4:0"]
    >>for d in device:
    >> nums = tuple(map(int, d.split(':')[1:]))
    >> print "for ", d, " : ", nums

    >
    >
    > Unfortunately, this doesn't work (even if I correct your typos) since
    > the delimeter isn't necessary a colon - that's exactly the difficulty I
    > am trying to solve.
    >

    In which case you'd better redefine """like "PCI:2:3.0" or
    "PCI:3.4:0"""" so we can understand the real problem :)

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
    Steve Holden, Sep 26, 2006
    #6
  7. Fabian Steiner a écrit :
    > Bruno Desthuilliers wrote:
    >
    >>Fabian Steiner wrote:
    >>
    >>>I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
    >>>need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
    >>>simple way to achieve this? So far I am using regular expressions but I
    >>>would like to avoid them ...

    >>
    >>devices = ["PCI:2:3.0", "PCI:3.4:0"]
    >>for d in device:
    >> nums = tuple(map(int, d.split(':')[1:]))
    >> print "for ", d, " : ", nums

    >
    >
    > Unfortunately, this doesn't work (even if I correct your typos) since
    > the delimeter isn't necessary a colon - that's exactly the difficulty I
    > am trying to solve.


    Hmmm, yes, sorry - didn't took time to test, so I missed this point.
    Bruno Desthuilliers, Sep 26, 2006
    #7
  8. wrote:
    > This may be a rare case where regular expressions are not a horrible,
    > self-defeating idea. Something like:
    >
    > delimiter = re.compile("[:\.]")
    > delimiter.split("PCI:2:3.0")
    > ...and then ignore the first entry, and map int the rest.
    > Alternatively, if the delimiters can really be anything, and if there
    > are no numbers in the first space ("PCI"), then maybe this approach:


    Thank you, this solution seems to be quite satisfying :)

    Regards,
    Fabian Steiner
    Fabian Steiner, Sep 26, 2006
    #8
  9. Fabian Steiner wrote:
    > I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
    > need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
    > simple way to achieve this? So far I am using regular expressions but I
    > would like to avoid them ...
    >
    > Regards,
    > Fabian Steiner


    I would personally go for regex, but what about a quick and dirty:

    s.replace('.',':').split(':')[1:]
    Virgil Dupras, Sep 26, 2006
    #9
  10. In message <efbl8h$kf2$03$-online.com>, Fabian Steiner wrote:

    > I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
    > need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
    > simple way to achieve this? So far I am using regular expressions but I
    > would like to avoid them ...


    Good for you for wanting to avoid REs if you can. Bookmark this page
    <http://docs.python.org/lib/string-methods.html> and refer to it often.
    Lawrence D'Oliveiro, Sep 27, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    4
    Views:
    363
    Jon A. Cruz
    Aug 24, 2003
  2. John Ericson
    Replies:
    0
    Views:
    419
    John Ericson
    Jul 19, 2003
  3. Mark
    Replies:
    0
    Views:
    434
  4. AG
    Replies:
    5
    Views:
    430
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Jan 18, 2007
  5. Tore Aursand

    Splitting an array into "even" parts

    Tore Aursand, Nov 5, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    186
    Tore Aursand
    Nov 5, 2003
Loading...

Share This Page