Python 3,7使用Re解码METAR信息

时间:2018-11-01 14:19:17

标签: python regex python-3.x

我一直试图将METAR气象信息代码解码为自由文本,并遇到问题。我正在将refindall函数一起使用,然后,如果发现任何问题,我正在对字符串进行索引以获取完全解码的metar消息,以分隔每个数字并在中间添加特定的单词。

我想知道是否有可能使代码更优雅,而不是将变量类型更改为字符串,然后引用其索引

import re

decoded_metar = ""
metar = "METAR XXXX 241100Z 30020KTG60 240V300 CAVOK 09/M00 Q1005 RMK 090 053 3/3="

wind = ""

wind_dir = re.compile("\d\d\d\d\dKT")
wind_speed = re.compile("\d\dKT")
wind_gusts = re.compile("G\d+")
wind_var = re.compile("\d{3}V\d{3}")

_wind_dir = str(wind_dir.findall(metar))
_wind_speed = str(wind_speed.findall(metar))
_wind_gusts = str(wind_gusts.findall(metar))
_wind_var = str(wind_var.findall(metar))

if _wind_dir == "['00000KT']":
    wind = "Wind calm. "
else:
    if len(_wind_dir) != 0:
        wind += "Wind " + _wind_dir[2] + " " + _wind_dir[3] + " " + _wind_dir[4] + " degrees "
    if len(_wind_speed) != 0:
        wind += "at " + _wind_speed[2] + " " + _wind_speed[3] + " knots. "
    if len(_wind_gusts) != 0:
        wind += "Gusting " + _wind_gusts[3] + " " + _wind_gusts[4] + " knots. "
    if len(_wind_var) != 0:
        wind += "Variable between " + _wind_var[2] + " " + _wind_var[3] + " " + _wind_var[4] + " degrees and " + _wind_var[6] + " " + _wind_var[7] + " " + _wind_var[8] + " degrees. "

print(wind)
print("End")

1 个答案:

答案 0 :(得分:0)

您必须使用re.search()。我在代码中详细说明了使用的常规表达式。也有用于测试的备用输入字符串。

    import re
    metar = "METAR XXXX 241100Z 30020KTG60 240V300 CAVOK 09/M00 Q1005 RMK 090 053 3/3="
    #metar = "METAR XXXX 241100Z 30020KT   240V300 CAVOK 09/M00 Q1005 RMK 090 053 3/3="
    #metar = "METAR XXXX 241100Z 30020KT           CAVOK 09/M00 Q1005 RMK 090 053 3/3="
    #metar = "METAR XXXX 241100Z 30020KT    CAVOK   240V300  09/M00 Q1005 RMK 090 053 3/3="

    # Expected output:
    # Wind 3 0 0 degrees at 2 0 knots. Gusting 6 0 knots. Variable between 2 4 0 degrees and 3 0 0 degrees.

    mo=re.search(r"(\d{3})(\d{2})KT(?:G(\d+))?(?:.+(\d{3})V(\d{3}))?",metar)
    print(mo.groups())    
    _wind_dir, _wind_speed, _wind_gusts, _wind_var_from, _wind_var_to= mo.groups()


    def stretch(s):
        return " ".join([c for c in s])


    wind=""
    if int(_wind_dir)==0 and int(_wind_speed)==0:
        wind = "Wind calm. "
    else:
        if int(_wind_dir) != 0:
            wind += "Wind " + stretch(_wind_dir) + " degrees "
        if int(_wind_speed) != 0:
            wind += "at " + stretch(_wind_speed) + " knots. "
        if _wind_gusts and int(_wind_gusts) != 0:
            wind += "Gusting " + stretch(_wind_gusts) + " knots. "
        if _wind_var_from:
            wind += "Variable between " + stretch(_wind_var_from) + " degrees and " + stretch(_wind_var_to) + " degrees. "

    print(wind+"\nEnd")

    """
    (\d{3})(\d{2})KT    This pattern is required.
                            (\d{3}) the 1st group for _wind_dir,
                            (\d{2}) the 2nd group for _wind_speed

    (?:G(\d+))?         (...G...)? There could be a pattern with 'G' and digits, or not.
                            (?: ...) The outer group's content is not interesting.
                            (\d+) group for _wind_gusts.

    (?:.+(\d{3})V(\d{3}))?  (...V...)? There could be a pattern with 'V' and digits, or not.
                                (?: ...) The outer group's content is not interesting.
                                .+ Some not interesting patterns.
                                (\d{3}) groups for variable values.
    """