转换度(度)字符gg°mm''ss'数据
问题描述:
我需要将格式为gg°mm"ss'
的坐标数据转换为十进制格式。 我在模块的开头使用# -*- coding: utf-8 -*- #
。转换度(度)字符gg°mm''ss'数据
在原文件中的数据线:
06:58 15:07:26 -53°08'00.7" -70°51'27.5" 2404.1 746.1 -2.4 22.3 0.3675
当文件被处理并消除空格和写其他与取景器行是:
06:58 15:07:26 -53∞08'00.7" -70∞51'27.5" 2404.1 746.1 -2.4 22.3 0.3765
我需要转换-53°08'00.7"
为十进制格式-gg,ddddd
。 但我不明白,因为在Spyder中是正确的,但没有发现。任何提示? 这是部分代码:
if os.path.exists(name):
with open(name, 'r', encoding="utf-8", errors="surrogateescape") as f:
for line in itertools.islice(f, 2, None): # start=2, stop=None
if not '//' in line:
linea1 = re.sub('[ \t]+' , ' ', line)
signo = linea1.find('-',0,6)
if signo == -1 :
file_mov.write(linea1)
答
查看是否存在以下帮助。这不是优雅的,但它应该给你一个关于正在发生的事情的好主意。
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import re
degree_sym = u'\N{DEGREE SIGN}'
sample = u' 06:58 15:07:26 -53\N{DEGREE SIGN}08\'00.7" -70\N{DEGREE SIGN}51\'27.5" 2404.1 746.1 -2.4 22.3 0.3675'
regex = r'(-?)(\d+)'+ degree_sym + r"(\d+)'" + r'(\d+|\d+\.\d+)"'
converted_words = []
for word in sample.split():
m = re.match(regex, word, flags=re.UNICODE)
if m:
sign = int(m.groups()[0]+'1')
degrees = float(m.groups()[1])
minutes = float(m.groups()[2])/60.0
seconds = float(m.groups()[3])/3600.0
result = "{0:.5f}".format(sign*(degrees+minutes+seconds))
converted_words.append(result)
else:
converted_words.append(word)
answer = " ".join(converted_words)
print(answer)
输出:
06:58 15:07:26 -53.13353 -70.85764 2404.1 746.1 -2.4 22.3 0.3675
+0
谢谢...我会尽力的 – Istari
请加在Python中尽最大努力的源代码,您似乎已经习惯(与和输出),后者制定明确的,缺什么,以一个完美的出来放。这将有助于帮助你的国际海事组织。 – Dilettant
你有什么错误?不知道'-gg,ddddd'是什么意思 - 你想要的输出是什么? – RobertB