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

王林
发布: 2024-02-11 14:24:04
转载
604人浏览过

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

问题内容

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

这是代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

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)

登录后复制

输入数据为:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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=

登录后复制

但是,它返回如下:

立即学习Python免费学习笔记(深入)”;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', '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 组),它返回:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

['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 脚本的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号