转换°(度)字符gg°mm''ss'd

2024-09-29 23:33:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要把坐标数据转换成十进制格式。 我在模块的开头使用# -*- 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

当处理文件并消除空格并使用查找器写入其他文件时,行为:

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)  

Tags: 模块文件数据nameinnoneif格式
1条回答
网友
1楼 · 发布于 2024-09-29 23:33:24

看看下面的方法是否有用。它不优雅,但它应该给你一个什么是正在发生的好主意。你知道吗

#! /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

相关问题 更多 >

    热门问题