搜尋
首頁後端開發Python教學用於讀取不同訊息模式的 Python 腳本

用於讀取不同訊息模式的 Python 腳本

Feb 11, 2024 pm 02:24 PM
蟒蛇腳本

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

問題內容

我正在嘗試製作一個靈活的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 腳本的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:stackoverflow。如有侵權,請聯絡admin@php.cn刪除
學習Python:2小時的每日學習是否足夠?學習Python:2小時的每日學習是否足夠?Apr 18, 2025 am 12:22 AM

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Web開發的Python:關鍵應用程序Web開發的Python:關鍵應用程序Apr 18, 2025 am 12:20 AM

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優

Python vs.C:探索性能和效率Python vs.C:探索性能和效率Apr 18, 2025 am 12:20 AM

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

python在行動中:現實世界中的例子python在行動中:現實世界中的例子Apr 18, 2025 am 12:18 AM

Python在現實世界中的應用包括數據分析、Web開發、人工智能和自動化。 1)在數據分析中,Python使用Pandas和Matplotlib處理和可視化數據。 2)Web開發中,Django和Flask框架簡化了Web應用的創建。 3)人工智能領域,TensorFlow和PyTorch用於構建和訓練模型。 4)自動化方面,Python腳本可用於復製文件等任務。

Python的主要用途:綜合概述Python的主要用途:綜合概述Apr 18, 2025 am 12:18 AM

Python在數據科學、Web開發和自動化腳本領域廣泛應用。 1)在數據科學中,Python通過NumPy、Pandas等庫簡化數據處理和分析。 2)在Web開發中,Django和Flask框架使開發者能快速構建應用。 3)在自動化腳本中,Python的簡潔性和標準庫使其成為理想選擇。

Python的主要目的:靈活性和易用性Python的主要目的:靈活性和易用性Apr 17, 2025 am 12:14 AM

Python的靈活性體現在多範式支持和動態類型系統,易用性則源於語法簡潔和豐富的標準庫。 1.靈活性:支持面向對象、函數式和過程式編程,動態類型系統提高開發效率。 2.易用性:語法接近自然語言,標準庫涵蓋廣泛功能,簡化開發過程。

Python:多功能編程的力量Python:多功能編程的力量Apr 17, 2025 am 12:09 AM

Python因其簡潔與強大而備受青睞,適用於從初學者到高級開發者的各種需求。其多功能性體現在:1)易學易用,語法簡單;2)豐富的庫和框架,如NumPy、Pandas等;3)跨平台支持,可在多種操作系統上運行;4)適合腳本和自動化任務,提升工作效率。

每天2小時學習Python:實用指南每天2小時學習Python:實用指南Apr 17, 2025 am 12:05 AM

可以,在每天花費兩個小時的時間內學會Python。 1.制定合理的學習計劃,2.選擇合適的學習資源,3.通過實踐鞏固所學知識,這些步驟能幫助你在短時間內掌握Python。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器