meuhfunk
meuhfunk

Reputation: 75

How to verify string type geographic coordinates are in the correct format?

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.

Upvotes: 1

Views: 351

Answers (1)

I'mahdi
I'mahdi

Reputation: 24059

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

Upvotes: 1

Related Questions