Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3,7使用Re解码METAR信息_Python_Regex_Python 3.x - Fatal编程技术网

Python3,7使用Re解码METAR信息

Python3,7使用Re解码METAR信息,python,regex,python-3.x,Python,Regex,Python 3.x,我一直在尝试将METAR meteo信息代码解码为自由文本,但遇到了一个问题。我使用 Re//> > FUNDALL 函数,然后如果发现任何东西,我使用字符串上的索引来获得完全解码的Meta消息,以分离每个数字,并在中间添加特定的单词。p> 我想知道是否有可能使代码更加优雅,而不是将变量类型更改为字符串,然后引用其索引 import re decoded_metar = "" metar = "METAR XXXX 241100Z 30020KTG60 240V300 CAVOK 09/M0

我一直在尝试将METAR meteo信息代码解码为自由文本,但遇到了一个问题。我使用<代码> Re//> > <代码> FUNDALL 函数,然后如果发现任何东西,我使用字符串上的索引来获得完全解码的Meta消息,以分离每个数字,并在中间添加特定的单词。p> 我想知道是否有可能使代码更加优雅,而不是将变量类型更改为字符串,然后引用其索引

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")
您必须使用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.
    """

如果将捕获组放入正则表达式中,则可以转换为
int
以获取值。此外,METAR不需要findall,因为每个表达式在一行中只出现一次。我应该使用哪个函数而不是findall?不幸的是,我需要字符串和分开的合成器pyttsx3讲它的每个数字。