用於讀取不同訊息模式的 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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

crontab定時任務不執行的一些緣由總結更新時間:2019年01月09日09:34:57作者:田野上的希望這篇文章主要給你們總結介紹了關於crontab定時任務不執行的一些緣由,對每種可能發生的誘因都給出了解決方式,對遇見這個問題的同事們具有一定的參考學習價值,須要的同學們下邊隨著小編來一起學習學習吧序言近來在工作中遇見了一些問題, crontab定時任務竟然不執行,後來我在網上找的時侯發覺網上主要說了這5個誘因:1crond服務未啟動crontab不是Linux內核的功能,而是依賴一個cron

Orange3是一個功能強大的開源資料視覺化和機器學習工具,它具備豐富的資料處理、分析和建模功能,為使用者提供了簡單快速的資料探勘和機器學習解決方案。本文將簡單介紹Orange3的基本功能與使用方法,同時結合實際應用場景與Python程式碼案例,幫助讀者更掌握Orange3的使用技巧。 Orange3的基本功能包括資料載入、資料預處理、特徵選擇、模型建立和評估等。使用者可以利用直覺的介面拖放元件,輕鬆建立資料流程。同時,透過Python腳本,也能完成更複雜的資料處理與建模任務。下面我們將通過一個實際

PyCharm是一款功能強大的Python整合開發環境,提供了豐富的功能和工具來幫助開發者提高效率。其中,PyInstaller是一個常用的工具,可以將Python程式碼打包為執行檔(EXE格式),方便在沒有Python環境的機器上運作。在本篇文章中,我們將介紹如何在PyCharm中使用PyInstaller將Python程式碼打包為EXE格式,並提供具體的

1.先開啟pycharm,進入到pycharm首頁。 2.然後新建python腳本,右鍵--點選new--點選pythonfile。 3.輸入一段字串,代碼:s="-"。 4.接著需要把字串裡面的符號重複20次,代碼:s1=s*20。5、輸入列印輸出代碼,代碼:print(s1)。 6.最後運行腳本,在最底部會看到我們的回傳值:-就重複了20次。

如何使用PyCharm讀取Excel資料?步驟如下:安裝openpyxl庫;匯入openpyxl庫;載入Excel工作簿;存取特定工作表;存取工作表中的儲存格;遍歷行和列。

CoreFreq:Linux下的CPU頻率監控工具介紹在Linux系統中,對於CPU頻率的監控與管理一直是比較重要的任務。透過監控CPU的頻率,我們可以隨時了解CPU的運作狀態,調整頻率以提高效能或降低功耗。在Linux系統中,有許多工具可以用來監控CPU頻率,其中一個比較優秀的工具是CoreFreq。本文將介紹CoreFreq工具的基本功能以及如何在L

網站子域名查詢工具有:1、Whois Lookup:可以查詢域名的註冊信息,包括子域名;2、Sublist3r:可以在搜尋引擎和其他工具的幫助下自動掃描域名的子域名;3、DNSdumpster:可以查詢網域的子網域、IP位址及DNS記錄等資訊;4、Fierce:可透過DNS伺服器查詢網域的子網域資訊:5、Nmap;6、Recon-ng;7、Google Hacking。

Flask安裝設定教學:輕鬆建置PythonWeb應用的利器,需要具體程式碼範例引言:隨著Python的日益流行,Web開發也成為了Python程式設計師的必備技能之一。而要進行Python的Web開發,我們需要選擇適合的Web框架。在眾多的PythonWeb框架中,Flask是一款簡潔、易上手且靈活的框架,備受開發者的青睞。本文將介紹Flask框架的安裝、
