Python --Notes when converting from str type to int type

Introduction

Converting from str type to int type is easy to see and unexpectedly error-prone. In this article, I have summarized the points to be aware of when converting from str type to int type and what to do about it.

Example where no error occurs

sample.py


data=int("1")
print(data)

Example of error

sample.py


data=int("1.0")
print(data)

solution

sample.py


data=int(float("1.0"))
print(data)

Can be solved by.

Finally

When I got the error for the first time, I tried my best to search, but I couldn't find any information. So, I wrote this article with the intention of increasing the information as much as possible.

Recommended Posts

Python --Notes when converting from str type to int type
Timezone specification when converting a string to datetime type in python
What I did when updating from Python 2.6 to 2.7
Changes from Python 2 to Python 3.0
How to convert Python # type for Python super beginners: str
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
Convert numpy int64 to python int
Cheating from PHP to Python
Python notes to forget soon
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Python immutable type int memo
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
How to convert Python # type for Python super beginners: int, float
Call Matlab from Python to optimize
Practice! !! Introduction to Python (Type Hints)
Create folders from '01' to '12' with python
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Things to note when running Python on EC2 from AWS Lambda
How to avoid duplication of data when inputting from Python to SQLite.
Connect to utf8mb4 database from python
Python (from first time to execution)
Try converting cloudmonkey CLI to python3 -1
Post images from Python to Tumblr
Notes on accessing dashDB from python
How to access wikipedia from python
Python to switch from another language
Precautions when using phantomjs from python
What to do when the value type is ambiguous in Python?
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
A story I was addicted to when inserting from Python to a PostgreSQL table
How to deal with OAuth2 error when using Google APIs from Python
[Python] Warning came out when I raised from Selenium 3 to 4 [Web Driver]
When running a Python shell from Electron, pass multiple arguments to run Python.
[Python] Fluid simulation: From linear to non-linear
[Introduction to Udemy Python3 + Application] 28. Collective type
From Python to using MeCab (and CaboCha)
Use PostgreSQL data type (jsonb) from Python
python notes: Modularization: __name__ == How to use'__main__'
Learning notes from the beginning of Python 1
Send a message from Python to Slack
Private Python handbook (updated from time to time)
[Introduction to Udemy Python3 + Application] 21. Tuple type
I want to use jar from python
Convert from katakana to vowel kana [python]
Push notification from Python server to Android
Connecting from python to MySQL on CentOS 6.4
Porting and modifying doublet-solver from python2 to python3.
How to access RDS from Lambda (python)
[Introduction to Udemy Python3 + Application] 24. Dictionary type
Python> Output numbers from 1 to 100, 501 to 600> For csv
[Updated from time to time] PostmarketOS related notes
Convert from Markdown to HTML in Python
[Amazon Linux] Switching from Python 2 series to Python 3 series
[Introduction to Udemy Python3 + Application] 16. List type
API explanation to touch mastodon from python
Learning notes from the beginning of Python 2
Connect to coincheck's Websocket API from Python
Notes from installing Homebrew to building an Anaconda environment for Python with pyenv