I have :
coordinates = '50.0572, 1.20575'
coordinates is string type.
50.0572 is latitude 1.2057 is longitude
I would like to find the simplest solution to verify that coordinates is in the format of type: XX.XX, XX.XX
Example : if coordinates = '120, 1.20' => False => bad format (error on 120)
Thank you.
CodePudding user response:
You can first .split(',')
then check .isdigit()
if get True
you have int
number and you find bad format
.
try this:
>>> coordinates = '120, 1.20'
>>> [cor.isdigit() for cor in coordinates.split(',')]
[True, False]
>>> coordinates = '50.0572, 1.20575'
>>> if not any(cor.isdigit() for cor in coordinates.split(',')):
... print("we don't have bad format")
we don't have bad format
If you want to use regex
you can use re.compile()
then use match
like below:
>>> import re
>>> flt_num = re.compile(r'\d .\d ')
>>> coordinates = '120, 1.20'
>>> for cor in coordinates.split(','):
... if flt_num.match(cor):
... print(f'{cor} has bad format')
120 has bad format