[英]How to verify string type geographic coordinates are in the correct format?
我有 :
coordinates = '50.0572, 1.20575'
坐标是字符串类型。
50.0572 是纬度 1.2057 是经度
我想找到最简单的解决方案来验证坐标的格式为:XX.XX, XX.XX
示例:如果坐标 = '120, 1.20' => False => 格式错误(120 上的错误)
谢谢你。
你可以先.split(',')
然后检查.isdigit()
如果得到True
你有int
number 并且你发现bad format
。
尝试这个:
>>> 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
如果你想使用regex
你可以使用re.compile()
然后使用如下match
:
>>> 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.