iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >用于读取不同消息模式的 Python 脚本
  • 605
分享到

用于读取不同消息模式的 Python 脚本

python脚本 2024-02-11 15:02:56 605人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

问题内容 我正在尝试制作一个灵活的python脚本,从synop代码中读取和提取一些天气变量。 这是代码: import re def extract_data_12_utc(mes

问题内容

我正在尝试制作一个灵活的python脚本,从synop代码中读取和提取一些天气变量。

这是代码:

import re

def extract_data_12_utc(message):
    # pattern message

    pattern = r'(\d{5}),(\d{4}),(\d{2}),(\d{2}),(\d{2}),(\d{2}),aaxx (\d{5}) (\d{5}) (\d{5}) (\d{5}) (1\d{4}) (2\d{4}) (3\d{4})? (4\d{4}) (6\d{4})? (7\d{4})? (8\d{4})? (\{3}) (2\d{4}) (5\d{4}) (7\d{4})'


    matches = re.search(pattern, message)

    # check if the match is successsful
    if matches:
        
        station = matches.group(1)
        year = matches.group(2)
        month = matches.group(3)
        day = matches.group(4)
        hour = matches.group(5)
        min = matches.group(6)

        # extracting variables
        temp_air = float(matches.group(11)[2:]) / 10.0
        temp_dew = float(matches.group(12)[2:]) / 10.0
        pres_station = float(matches.group(13)[1:]) / 10.0 + 1000  
        pres_sealv = float(matches.group(14)[1:]) / 10.0 + 1000
        prec_6h = float(matches.group(15)[2:4]) if matches.group(15) else none
        wx = str(matches.group(16)[1:]) if matches.group(16) else none
        cld = str(matches.group(17)[1:]) if matches.group(17) else none
        temp_min = float(matches.group(19)[2:]) / 10.0 if matches.group(19) else none
        pres_chg = float(matches.group(20)[2:]) / 10.0 if matches.group(20) else none
        prec_24h = float(matches.group(21)[1:]) / 10.0 if matches.group(21) else none

        # fORMatting results
        formatted_data = [
            station, year, month, day, hour, min,
            f"{int(temp_air):02d}.{int((temp_air % 1) * 10):01d}",
            f"{int(temp_dew):02d}.{int((temp_dew % 1) * 10):01d}",
            f"{int(pres_station):04d}.{int((pres_station % 1) * 10):01d}",
            f"{int(pres_sealv):04d}.{int((pres_sealv % 1) * 10):01d}",
            f"{int(prec_6h):1d}"  if prec_6h is not none else "none",
            f"{int(wx):1d}"  if wx is not none else "none",
            f"{int(cld):1d}"  if cld is not none else "none",
            f"{int(temp_min):02d}.{int((temp_min % 1) * 10):01d}",
            f"{int(pres_chg):1d}"  if pres_chg is not none else "none",
            f"{prec_24h:.1f}" if prec_24h is not none else "none"
        ]

        # returns formatted data
        return formatted_data
    else:
        # returns list if fails
        return ["none"] * 16

# reading file
file_name = r"synop.txt"
with open(file_name, 'r') as file:
    lines = file.readlines()

# list to store results
data_12_utc = []

# from 17th line
for line in lines:
    data = extract_data_12_utc(line)
    data_12_utc.append(data)

# show formatted data
for data in data_12_utc:
    print(data)

输入数据为:

82145,2024,01,24,12,00,aaxx 24124 82145 32598 30502 10292 20250 30082 40124 83200 333 20231 58004=
82181,2024,01,24,12,00,aaxx 24124 82181 21498 73603 10257 20242 30008 40149 70262 84520 333 20246 59014 60084=
82184,2024,01,24,12,00,aaxx 24124 82184 21498 60502 10272 20252 30116 40124 70362 85520 333 20243 59014 69944=
82188,2024,01,24,12,00,aaxx 24124 82188 11560 53602 10264 20248 30128 40146 60214 72162 83260 333 58002 70210==
82191,2024,01,24,12,00,aaxx 24124 82191 12570 60501 10290 20262 30108 40114 60184 84250 333 20238 59014 70180==
82193,2024,01,24,12,00,aaxx 24124 82193 22470 30409 10289 20254 30106 40124 83100 333 20254 59016 60054=
82244,2024,01,24,12,00,aaxx 24124 82244 11470 70503 10269 20248 30061 40130 60024 70296 84220 333 20256 59002 70020==
82246,2024,01,24,12,00,aaxx 24124 82246 21596 83202 10252 20242 3//// 4//// 7036/ 887// 333 2//// 5//// 60254=
82263,2024,01,24,12,00,aaxx 24124 82263 11470 8//// 30118 69934 70352 887// 333 59013 70003==
82353,2024,01,24,12,00,aaxx 24124 82353 22497 63602 10264 20246 30002 40086 86400 333 20215 59014 60024=
82361,2024,01,24,12,00,aaxx 24124 82361 21497 63602 10276 20258 30088 40125 70265 86700 333 20269 59018 60024=
82444,2024,01,24,12,00,aaxx 24124 82444 12470 72703 10269 20252 30091 60624 85000 333 20270 58000 70620==
82445,2024,01,24,12,00,aaxx 24124 82445 22497 83202 10266 20254 30102 40154 8472/ 333 20243 58000 60314=
82562,2024,01,24,12,00,aaxx 24124 82562 32597 836// 1//// 2//// 3//// 4//// 8869/ 333 2//// 5////=
82861,2024,01,24,12,00,aaxx 24124 82861 21596 73202 1//// 2//// 39917 4//// 70360 8572/ 333 2//// 59027 60054=

但是,它返回如下:

['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']

当我限制某些变量时(即直到第 15 组),它返回:

['82145', '2024', '01', '24', '12', '00', '29.1', '25.0', '1008.2', '1012.3', 'None']
['82181', '2024', '01', '24', '12', '00', '25.6', '24.1', '1000.7', '1014.8', 'None']
['82184', '2024', '01', '24', '12', '00', '27.1', '25.1', '1011.6', '1012.3', 'None']
['82188', '2024', '01', '24', '12', '00', '26.3', '24.8', '1012.7', '1014.6', '21']
['82191', '2024', '01', '24', '12', '00', '29.0', '26.1', '1010.7', '1011.3', '18']
['82193', '2024', '01', '24', '12', '00', '28.8', '25.3', '1010.6', '1012.3', 'None']
['82244', '2024', '01', '24', '12', '00', '26.8', '24.8', '1006.1', '1013.0', '2']
['None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None']
['None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None']
['82353', '2024', '01', '24', '12', '00', '26.3', '24.6', '1000.2', '1008.6', 'None']
['82361', '2024', '01', '24', '12', '00', '27.6', '25.8', '1008.7', '1012.5', 'None']
['None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None']
['82445', '2024', '01', '24', '12', '00', '26.6', '25.3', '1010.2', '1015.3', 'None']
['None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None']
['None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None']
['None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None']
['None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None']

我如何拥有一个包含所有类型的模式消息的脚本?


正确答案


即使只有一个变量格式错误,也可能有理由拒绝整行(或用 None 字符串替换)。

但是,如果您想提取每个格式正确的变量,即使行中的某些变量格式错误,您也应该使用 re.split(', ', line) 将行拆分为变量列表,并分别转换/检查每个变量。不幸的是,re 匹配整个表达式而不是每个组

如果您必须使用一种灵活的正则表达式,则应考虑使用 (?:(4\d{4})|\d*[/]+) 之类的潜在格式错误的组。 遗憾的是,它增加了组的数量,因此我使用非捕获组运算符 :? 来保持组编号相同。如果您发现它太笨拙,另一种选择是使用更多通用组表达式 (4[/\d]{4}),它允许缺失值,但稍后您将测试是否存在缺失的数字符号“/”或只是在转换过程中捕获异常。

以上就是用于读取不同消息模式的 Python 脚本的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: 用于读取不同消息模式的 Python 脚本

本文链接: https://www.lsjlt.com/news/563503.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作