osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

best way to remove leading zeros from a tuple like string


On Sun, May 20, 2018 at 12:54 PM,  <bruceg113355 at gmail.com> wrote:
> Lets say I have the following tuple like string.
>   (128, 020, 008, 255)
>
> What is the best way to to remove leading zeroes and end up with the following.
>   (128, 20, 8, 255)        -- I do not care about spaces
>
> This is the solution I came up with
>     s = "(128, 020, 008, 255)"
>     v = s.replace ("(", "")
>     v = v.replace (")", "")
>     vv = ",".join([str(int(i)) for i in v.split(",")])
>     final = "(" + vv + ")"

I actually like your solution pretty well - especially the splitting
on "," and applying int() to the pieces. If you want a simple regex
solution though, the following should work on Python 1.5 through
3.7b3:

$ pythons --command 'import re; print("(" + re.sub("(\(| )0*", "",
"(128, 020, 008, 255)"))'
below cmd output started 2018 Wed May 23 01:23:00 PM PDT
/usr/local/cpython-1.0/bin/python (1.0.1) bad
    Traceback (innermost last):
      File "<string>", line 1
    ImportError: No module named re
/usr/local/cpython-1.1/bin/python (1.1) bad
    Traceback (innermost last):
      File "<string>", line 1, in ?
    ImportError: No module named re
/usr/local/cpython-1.2/bin/python (1.2) bad
    Traceback (innermost last):
      File "<string>", line 1, in ?
    ImportError: No module named re
/usr/local/cpython-1.3/bin/python (1.3) bad
    Traceback (innermost last):
      File "<string>", line 1, in ?
    ImportError: No module named re
/usr/local/cpython-1.4/bin/python (1.4) bad
    Traceback (innermost last):
      File "<string>", line 1, in ?
    ImportError: No module named re
/usr/local/cpython-1.5/bin/python (1.5.2) good (128,20,8,255)
/usr/local/cpython-1.6/bin/python (1.6.1) good (128,20,8,255)
/usr/local/cpython-2.0/bin/python (2.0.1) good (128,20,8,255)
/usr/local/cpython-2.1/bin/python (2.1.0) good (128,20,8,255)
/usr/local/cpython-2.2/bin/python (2.2.0) good (128,20,8,255)
/usr/local/cpython-2.3/bin/python (2.3.0) good (128,20,8,255)
/usr/local/cpython-2.4/bin/python (2.4.0) good (128,20,8,255)
/usr/local/cpython-2.5/bin/python (2.5.6) good (128,20,8,255)
/usr/local/cpython-2.6/bin/python (2.6.9) good (128,20,8,255)
/usr/local/cpython-2.7/bin/python (2.7.13) good (128,20,8,255)
/usr/local/cpython-3.0/bin/python (3.0.1) good (128,20,8,255)
/usr/local/cpython-3.1/bin/python (3.1.5) good (128,20,8,255)
/usr/local/cpython-3.2/bin/python (3.2.5) good (128,20,8,255)
/usr/local/cpython-3.3/bin/python (3.3.3) good (128,20,8,255)
/usr/local/cpython-3.4/bin/python (3.4.2) good (128,20,8,255)
/usr/local/cpython-3.5/bin/python (3.5.0) good (128,20,8,255)
/usr/local/cpython-3.6/bin/python (3.6.0) good (128,20,8,255)
/usr/local/cpython-3.7/bin/python (3.7.0b3) good (128,20,8,255)
/usr/local/jython-2.7/bin/jython (2.7.0) good (128,20,8,255)
/usr/local/pypy-5.10.0/bin/pypy (2.7.13) good (128,20,8,255)
/usr/local/pypy-5.3.1/bin/pypy (2.7.10) good (128,20,8,255)
/usr/local/pypy-5.9.0/bin/pypy (2.7.13) good (128,20,8,255)
/usr/local/pypy-6.0.0/bin/pypy (2.7.13) good (128,20,8,255)
/usr/local/pypy3-5.10.0/bin/pypy3 (3.5.3) good (128,20,8,255)
/usr/local/pypy3-5.5.0/bin/pypy3 (3.3.5) good (128,20,8,255)
/usr/local/pypy3-5.8.0-with-lzma-fixes/bin/pypy3 (3.5.3) good (128,20,8,255)
/usr/local/pypy3-5.8.0/bin/pypy3 (3.5.3) good (128,20,8,255)
/usr/local/pypy3-5.9.0/bin/pypy3 (3.5.3) good (128,20,8,255)
/usr/local/pypy3-6.0.0/bin/pypy3 (3.5.3) good (128,20,8,255)
/usr/local/micropython-git-2017-06-16/bin/micropython (3.4.0) good
(128,20,8,255)

HTH