Reputation: 53
Here is an example of the data:
x = '[ 5 | 6 | 7 [' # number 1
y = '[ 10 | 11 | 7 [' # number 2
For number one, I just want 5. Number 2, I just want 10. How can I do this?
Upvotes: 0
Views: 50
Reputation: 168736
You seem to want to isolate the 2nd space-separated field. Use str.split()
for this:
for s in ('[ 5 | 6 | 7 [', '[ 10 | 11 | 7 ['):
print s, " => ", int(s.split()[1])
If you want all of the numeric fields, use str.isdigit()
to determine which fields are numeric, and filter()
to create a new list:
for s in ('[ 5 | 6 | 7 [', '[ 10 | 11 | 7 ['):
print filter(str.isdigit, s.split())
If you don't know if there are spaces around each number, you can isolate the numbers using re.findall()
:
for s in ('[ 5 | 6 | 7 [', '[ 10 | 11 | 7 ['):
print re.findall('\d+', s)
Upvotes: 2