Splitting device addresses into parts

F

Fabian Steiner

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
 
B

Bruno Desthuilliers

Fabian said:
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
 
F

Fabian Steiner

Bruno said:
Fabian said:
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
 
J

johnzenger

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 said:
Bruno said:
Fabian said:
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
 
P

Paddy

Fabian said:
Bruno said:
Fabian said:
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.
 
S

Steve Holden

Fabian said:
Bruno said:
Fabian said:
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
 
B

Bruno Desthuilliers

Fabian Steiner a écrit :
Bruno said:
Fabian said:
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.
 
F

Fabian Steiner

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
 
V

Virgil Dupras

Fabian said:
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:]
 
L

Lawrence D'Oliveiro

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top