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:
The weird thing is, when I change the slicing operation of month to
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:
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)Unfortunately I get the following error:
month = int(month) ValueError: invalid literal for int() with base 10: ''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
month = date[0:5]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?