Kauê Gimenes
Kauê Gimenes

Reputation: 1286

Python Divide a string

I have a string like this:

 <td casd2" aasdeft" class="satyle3">
    <b><a asddidasd?ct=Peasds&amp;fasdaao=Monsdar
    &amp;pID=19635"...

I need the 19635.

Someone can help me ?

Upvotes: 1

Views: 678

Answers (3)

Karthik Gurusamy
Karthik Gurusamy

Reputation: 769

If you are parsing HTML/XML, it's best to use the right tool. re can get the job done quick and dirty; but will come back to bite you when you extend later (software that is not dead always evolves) or you need to handle other forms of representation of the same data.

Beautiful Soup in python provides good parsing routines -- it's worth going thru' the learning curve.

Upvotes: 1

juliomalegria
juliomalegria

Reputation: 24921

I would use regular expressions to make a more neat solution:

>>> import re
>>> s = '<td casd2" aasdeft" class="satyle3"><b><a asddidasd?ct=Peasds&amp;fasdaao=Monsdar&amp;pID=19635"...'
>>> match = re.search(".*pID=(\d+).*",s)
>>> if match:
...   match.group(1)
... 
'19635'

Nice and simple isn't it?

Upvotes: 3

rossipedia
rossipedia

Reputation: 59407

With what little information given, this is how I'd approach it:

import re

someString = ... # your original string

m = re.search(r"pID=(\d+)", someString)
pid = m.group(1)

Upvotes: 3

Related Questions