Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Splitting device addresses into parts

Reply
Thread Tools

Splitting device addresses into parts

 
 
Fabian Steiner
Guest
Posts: n/a
 
      09-26-2006
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
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-26-2006
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 '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
 
Fabian Steiner
Guest
Posts: n/a
 
      09-26-2006
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
 
Reply With Quote
 
johnzenger@gmail.com
Guest
Posts: n/a
 
      09-26-2006
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


 
Reply With Quote
 
Paddy
Guest
Posts: n/a
 
      09-26-2006

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.

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      09-26-2006
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

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-26-2006
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.
 
Reply With Quote
 
Fabian Steiner
Guest
Posts: n/a
 
      09-26-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Virgil Dupras
Guest
Posts: n/a
 
      09-26-2006
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:]

 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      09-27-2006
In message <efbl8h$kf2$03$(E-Mail Removed)-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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parts parts....PARTS!!! ARGHHH dstvns A+ Certification 8 01-07-2004 07:57 PM
(easy) splitting a file name into parts Andy Fish Java 4 08-24-2003 05:49 AM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments