4

I'm pretty new to python. I have a question. Say for example when I read a line from a file I have a string that looks like this.

thestring = '000,5\r\n'

How do I remove all non-integers from this string and then convert this string into an integer itself? Thanks!

2
  • 4
    Do you need to be able to read negatives, hex (0xFFFFFF), or floats (-592.45821543e+04)? Commented Jun 18, 2013 at 20:30
  • Just nitpicking, but you do not want to remove non-integers, but non-digits Commented Jun 19, 2013 at 15:27

1 Answer 1

11

Using str.translate, this is probably the fastest way to do this:

>>> strs = '000,5\r\n'    
>>> from string import ascii_letters, punctuation, whitespace
>>> ignore = ascii_letters + punctuation + whitespace
>>> strs.translate(None, ignore)
'0005'

Using regex:

>>> import re
>>> re.sub(r'[^\d]+','',strs)    #or re.sub(r'[^0-9]+','',strs)
'0005'

Using str.join and str.isdigit:

>>> "".join([x for x in strs  if x.isdigit()])
'0005'

Use int() to get the integer:

>>> int('0005')
5

Timing comparisons:

>>> strs = strs*10**4
>>> %timeit strs.translate(None, ignore)
1000 loops, best of 3: 441 us per loop

>>> %timeit re.sub(r'[^\d]+','',strs)
10 loops, best of 3: 20.3 ms per loop

>>> %timeit re.sub(r'[^0-9]+','',strs)
100 loops, best of 3: 17.1 ms per loop

>>> %timeit "".join([x for x in strs  if x.isdigit()])
10 loops, best of 3: 19.2 ms per loop
Sign up to request clarification or add additional context in comments.

6 Comments

Or translate if you need something slightly faster.
Great Thanks! Now suppose my my string is '0005' How can I reverse my string so that it now reads '5000' ?
@Binka use this : '0005'[::-1]
Out of curiosity what is the difference in us and ms?
@johnthexiii us is microsecond ((10^-6)sec) and ms is millisecond((10^-3)sec).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.