Jul-17-2020, 06:16 PM
Hello,
I'm kind of feeling Python is making fun of me. I have strings with dates in the format "20200716", stored as the values in a dictionary.
With a for-loop I am extracting them and slice them into year, month and day. In order to be able to pass them onto the datetime.date-function (that's why I remove the leading zeros from month and day), I want to convert them to integers. So far so easy:
Unfortunately I get the following error:
I've already printed "month" out, it is a digit. I've checked with isdigit(), True, it is a number.
The weird thing is, when I change the slicing operation of month to
it works just fine. Although now I don't have the isolated month.
The online difference is the [0:5] instead of [3:5].
What the heck is going on here?
I'm kind of feeling Python is making fun of me. I have strings with dates in the format "20200716", stored as the values in a dictionary.
With a for-loop I am extracting them and slice them into year, month and day. In order to be able to pass them onto the datetime.date-function (that's why I remove the leading zeros from month and day), I want to convert them to integers. So far so easy:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
year = str () month = str () day = str () for k,v in my_dict.items(): date = str (v) year = date[: 4 ] month = date[ 3 : 5 ] day = date[ 5 :] month = month.lstrip( "0" ) day = day.lstrip( "0" ) year = int (year) month = int (month) day = int (day) |
1 2 |
month = int (month) ValueError: invalid literal for int () with base 10 : '' |
The weird thing is, when I change the slicing operation of month to
1 |
month = date[ 0 : 5 ] |
The online difference is the [0:5] instead of [3:5].
What the heck is going on here?