[英]convert ° (degree) character gg°mm'' ss' data
我需要將格式為gg°mm"ss'
坐標數據轉換為十進制格式。我在模塊頂部使用# -*- coding: utf-8 -*- #
。
原始文件中的數據行:
06:58 15:07:26 -53°08'00.7" -70°51'27.5" 2404.1 746.1 -2.4 22.3 0.3675
處理文件並消除空格並用finder寫其他文件時,該行是:
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中是正確的,但在finder中不是。有任何提示嗎?這是代碼的一部分:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.