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 9 1199
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 'o****@xiludom.gro'.split('@')])"
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
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
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.
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
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. jo********@gmail.com 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 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:]
In message <ef*************@news.t-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.htmland refer to it often. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sandman |
last post by:
I am splitting a text block into paragraphs, to be able to add images and stuff
like that to a specific paragraph in a content management system.
Well, right now I'm splittin on two or more...
|
by: Piotr |
last post by:
Is there any way to split all merged words but www and e-mail addresses?
I have regexp
preg_replace("/(\.)(])/", "\\1 \\2", "www.google.com
any,merged.words mymail@domain.com")
it give me...
|
by: Matt |
last post by:
Hi,
I'm ridiculously new to Access (about a week!) so please be patient!
My database is a record of British Standards. Each has a unique
identifier. Some are split into parts. I would like...
|
by: CharChabil |
last post by:
Using Vb.net 2005,
I want to read each part in this string in an array (splitting the string)
-----------
A1/EXT "BK82 LB73 21233" 105 061018 1804
-----------
That Code that i used is as follow:...
|
by: Matik |
last post by:
Hi to everyone,
My problem is, that I'm not so quite sure, which way should I go.
The user is inputing by second part application a long string (let's
say 128 characters), which are separated...
|
by: jb |
last post by:
Am using the 'Web Application Project' model for an asp.net web app.
Sections of this now need to be reusable. Is it possible to split into
sub projects? What happens with /bin directory if so -...
|
by: SungHyun Nam |
last post by:
Hello,
If there are two ethernet device, how I can select a device to send/receive?
Actually I want to do loopback test between twe ethernet device.
Send a UDP packet through eth0 and receives...
|
by: Steven D'Aprano |
last post by:
I'm trying to split a URL into components. For example:
URL = 'http://steve:secret@www.domain.com.au:82/dir" + \
'ectory/file.html;params?query#fragment'
(joining the strings above with plus...
|
by: David Jackson |
last post by:
Hello,
The company I'm working for has taken over a smaller company with a fairly
large customer base. We want to send an email to that customer base
informing them of the takeover but the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |