Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python catch int conversion error

>>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
...     try:
...         print('%s as an int is %d' % (str(value), int(value)))
...     except ValueError as ex:
...         print('"%s" cannot be converted to an int: %s' % (value, ex))
...
12345 as an int is 12345
67890 as an int is 67890
3.14 as an int is 3
42 as an int is 42
21 as an int is 21
254 as an int is 254
"Not convertible" cannot be converted to an int: invalid literal for int() with base 10: 'Not convertible'
Comment

PREVIOUS NEXT
Code Example
Python :: SciPy Spatial Data 
Python :: check dictionary values pandas dataframe colu 
Python :: Python Generators with a Loop 
Python :: remove element from list python by value 
Python :: python coding practice 
Python :: django query filter greater than or equal to 
Python :: insertion sort 
Python :: Generate bootstrap sample 
Python :: how to slice few rows in pandas 
Python :: standard deviation in python without numpy 
Python :: how to sort a list 
Python :: maximum recursion depth exceeded while calling a Python object 
Python :: Split a list based on a condition 
Python :: false in py 
Python :: how to add array and array in python 
Python :: python how to make boxplots with jitter 
Python :: append to an array in 1st place python 
Python :: nltk python how to tokenize text 
Python :: how to make simple login in python 
Python :: python web app 
Python :: get maximum value index after groupby 
Python :: Generate bar plot python 
Python :: remove emoji 
Python :: os.filename 
Python :: how to create fastapi 
Python :: tkinter radio button default selection 
Python :: python startswith 
Python :: python pip 
Python :: python qr scanner 
Python :: double for loop in list comprehension 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =