Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert str to int
#1
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:
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?
Reply
#2
please show example of original format and expected output.
if it's a string to start with, why not slice the string first and then convert components to integers?
Reply
#3
Year is [:4] and month is [3:5]. If 20200716 is given then month will be 00. If one strips zeros from that one will get empty string and this is exactly what this ValueError is about.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
the indexes for month are incorrect
>>> spam = '20200716'
>>> spam[3:5]
'00'
also, that's complicated conversion
>>> from datetime import datetime
>>> eggs = datetime.strptime(spam, '%Y%m%d')
>>> eggs.year
2020
>>> type(eggs.year)
<class 'int'>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
>>> mydate = '20200716'
>>> year = mydate[:4]
>>> year
'2020'
>>> month = mydate[4:-2]
>>> month
'07'
>>> day = mydate[-2:]
>>> day
'16'
Reply
#6
from datetime import date

timestamp = "20200716"
d = date(int(timestamp[:4]), int(timestamp[4:6]), int(timestamp[6:8]))
dt = d.strftime("%-d.%B %Y")
print(dt)
Output:
16.July 2020
Reply
#7
Pendulum is what i use most now as dos dates better than everything else in Python.
>>> import pendulum
>>> 
>>> date = "20200716"
>>> date = pendulum.parse(date)
>>> date
DateTime(2020, 7, 16, 0, 0, 0, tzinfo=Timezone('UTC'))
>>> print(date)
2020-07-16T00:00:00+00:00
>>> 
>>> date.year
2020
>>> date.month
7
>>> date.day
16
>>> 
>>> date.to_day_datetime_string()
'Thu, Jul 16, 2020 12:00 AM'
>>> date.to_rfc850_string()
'Thursday, 16-Jul-20 00:00:00 UTC'
>>> date.to_atom_string()
'2020-07-16T00:00:00+00:00'
>>> date.to_date_string()
'2020-07-16'
>>> 
>>> now = pendulum.now()
>>> print(now)
2020-07-17T21:32:55.008649+02:00
>>> now.diff_for_humans(date)
'1 day after'
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020