[PYTHON] What to do when "TypeError: must be string, not int…" appears when using strptime

Background and problems

I thought it would be useful to have a script that automatically enters the date of attendance management. I wrote the following using strptime of python's datetime module.

Kinkyu.py


input_date = input('Please enter the year and month to create(Example:201608):')

try:
    month_first = datetime.datetime.strptime(input_date, '%Y%m')
except ValueError:
	    input('Enter the year and month as 201608.')
	    sys.exit()

Then, the following error occurs when entering the year and month.

month_first = datetime.datetime.strptime(input_date, '%y%m')
TypeError: must be string, not int

Cause

that? I thought and looked it up Apparently, the first argument of strptime is a character string. You can't use numbers ...

approach

If the int type is NG, why not convert it to a String type? I thought I changed it as follows.

Kinkyu.py


input_date = input('Please enter the year and month to create(Example:201608):')
input_date = str(input_date) #Changes: Convert numbers to strings

try:
    month_first = datetime.datetime.strptime(input_date, '%Y%m')
except ValueError:
	    input('Enter the year and month as 201608.')
	    sys.exit()

This is the solution. Type conversion is easy with python.

Reference link

Recommended Posts

What to do when "TypeError: must be string, not int…" appears when using strptime
What to do when "Type Error: must be _socket.socket, not socket" appears on GAE
What to do when "TypeError: data type not understood" appears in python's numpy.zeros
What to do when is not in the sudoers file.This incident will be reported.
What to do when "SSL: CERTIFICATE_VERIFY_FAILED _ssl.c: 1056" appears in Python
OSError: [Errno 40] What to do when Message too long appears
What to do when "Invalid HTTP_HOST header" appears in Django
What to do when Ubuntu crashes
What to do when the jupyterlab extension settings are not reflected
What to do if Insecure Platform Warning appears when running Python
[virtualbox] What to do when [Could not retrieve mirrorlist] appears when yum update is performed on CentOS7
[Memorandum] What to do when a warning appears after executing pip list
What to do when pyinstaller: error: argument --add-binary: invalid add_data_or_binary value: appears
[Docker] What to do when error Couldn't find the binary git appears
What to do when "OS Error: [WinError 126] The specified module cannot be found" appears in import torch
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
Notes on what to do when matplotlib scatter () / scatter3d () does not work
What to do when a warning appears around Python integration in Neovim's CheckHealth
Python 3 migration> TypeError: must be real number, not map support> change map () to list (map ())
[Ubuntu 18.04 LTS] What to do when the screen resolution cannot be selected [NVIDIA]
What to do if you get "Python not configured." Using PyDev in Eclipse
What to do when [Errno 2] No such file or directory appears in Python
What to do when the graph does not appear in jupyter (ipython) notebook
What to do if NotADirectoryError: [Errno 20] Not a directory:'xdg-settings' appears in jupyter notebook
[EC2] What to do when selenium is stuck and processing does not proceed
What to do if pip cannot be installed
What to do when PermissionError of tempfile.mkstemp occurs
[Python] Type Error:'WebElement' object is not iterable What to do when an error occurs
What to do when python3 type venv does not work well on Raspberry Pi
[Python] What to do when No module named'pyproj.datadir' appears when Exe is done with PyInstaller
What to do when Python starts up in Anaconda does not come out unexpectedly
What to do if you don't want to use Japanese column names when using ortoolpy.logistics_network
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
curl: (60) What to do when Issuer certificate is invalid.
What to do when gdal_merge creates a huge file
What to do when raise ValueError, "unsupported hash type"
It was a painful memory when I was told TypeError: must be type, not class obj when trying to inherit with Python.
What to do when "cannot import name xxx" [Python]
What to do if sys / cdefs.h does not exist
What I was addicted to when using Python tornado
What to do if pyenv is not enabled (zsh)
What to do when you can't bind CaboCha to Python
What to do if the image is not displayed using matplotlib etc. in the Docker container
What to do when a Missing artifact occurs in a jar that is not defined in pom.xml
What to do if ʻObject arrays cannot be loaded when allow_pickle = False` occurs in numpy.load ()
[AWS] What to do when you want to pip with Lambda
How to check when python-memcached server could not be connected
What to do if the package dependency cannot be repaired
How to not load images when using PhantomJS with Selenium
What to do when PyCharm font is strange or garbled
What to do when a Remove Error occurs when updating conda
What to do when Unalignable boolean Series provided as indexer
TypeError: unsupported operand type (s) for-: What to do when'datetime.time'and'datetime.time'
What to do when an error occurs with import _ssl
What to do if package installation fails when deploying to heroku
ImportError: No module What to do when you are told
[Beginner] What to do when "[Errno 2] File b'test.csv' does not exist: b'test.csv" is displayed when reading pandas csv
What to do if you get angry with'vertices' must be a 2D list ... in matplotlib arrow
[VSCode] unable to import'google.cloud' What to do when pylint (import-error) does not recognize the Python import statement
What to do if you get a must override `get_config` error when trying to model.save in Keras
NameError: global name'dot_parser' is not defined and what to do when it comes up in python