Is there any reason why lVals = [1, 08, 2011] throws an exception?

前端 未结 4 857
温柔的废话
温柔的废话 2021-01-17 20:06

I have discovered one thing that makes me crazy. If I specify the following list:

lVals = [1, 01, 2011]

then no errors will be displayed, a

相关标签:
4条回答
  • 2021-01-17 20:36

    I guess what you're trying to do is split a date and put it in a list. This is what works for me:

    >>> date = "28-08-2011".split("-")
    >>> for i, num in enumerate(date):
    ...     date[i] = int(num, 10) # changes octal to decimal, thus losing the prefix 0
    ... 
    >>> date
    [28, 8, 2011]
    
    0 讨论(0)
  • 2021-01-17 20:43

    In Java, the zero prefix would specify an Octal value - so 01...07 are fine, 08 would be an error as there is no 8 in Octal.

    0 讨论(0)
  • 2021-01-17 20:54

    I don't really know Python, but I'd guess it takes the starting 0 as the beginning of an octal literal.

    0 讨论(0)
  • 2021-01-17 20:58

    08 is attempting to parse 8 as an octal digit. It isn't one.

    0 讨论(0)
提交回复
热议问题