Search code examples
pythondictionaryleading-zero

Python storing number with leading zeros in Dictionary


I have a dictionary called table. I want to assign this number to the below dic key. it keeps giving me an error saying "invalid token". I have tried converting it string, int, and float but to no avail

table['Fac_ID'] = 00000038058


Solution

  • Don't convert it to a string, use it as a string in the first place:

    >>> table['Fac_ID'] = str(00000038058)
      File "<stdin>", line 1
        table['Fac_ID'] = str(00000038058)
                                        ^
    SyntaxError: invalid token
    >>> table['Fac_ID'] = '00000038058'
    >>> print table['Fac_ID']
    00000038058
    

    str, as any function, evaluates the argument to a value before passing it in, so if there was an invalid token before str, using str is not going to change that. You need to use a valid token, so just hardcode the string.