iT邦幫忙

0

python讀檔-抓取指定資料

  • 分享至 

  • xImage

使用python抓取讀檔紀錄中指定的字串:
原本log:"2024-08-01 09:18:53,093 INFO [stdout] 2024-08-01 09:18:53 INFO (AuthenticationHandler.java:XXX) [AuthenticationHandler.authenticate] ProductName:BPM.JAVA Date:08-01-2024 Time:09:18:53 ProgID:AuthenticationHandler ClientIP:XXX.XX.XXX.XX WebServer:bpm.oitc.com.tw:XXXX UserID:11111 UserName:11111 UserOID: Type:Login TargetID:11111 TargetName:11111 LicenseNotEnoughException"
需抓取內容:Date、Time、UserID(這三個-其他都不用)
=>目前匯出的格式如下:
"Filename: server.log.2024-08-01 | Date: 08-01-2024 Time:09:16:48 ProgID:AuthenticationHandler ClientIP:XXX.XX.XXX.XX WebServer:bpm.oitc.com.tw:XXXX UserID:11111 UserName:11111 UserOID: Type:Login TargetID:11111 TargetName:11111 LicenseNotEnoughException | Time: 09:16:48 ProgID:AuthenticationHandler ClientIP:XXX.XX.XXX.XX WebServer:bpm.oitc.com.tw:XXXX UserID:11111 UserName:11111 UserOID: Type:Login TargetID:11111 TargetName:11111 LicenseNotEnoughException | UserID: 11111 UserName:11111 UserOID: Type:Login TargetID:11111 TargetName:11111 LicenseNotEnoughException | Error: LicenseNotEnoughException"

程式碼如附件https://ithelp.ithome.com.tw/upload/images/20240926/201697813WlijHKXhl.png

ccutmis iT邦高手 2 級 ‧ 2024-09-26 12:05:02 檢舉
output_line = f"Date: {date} | Time: {time} | UserID: {user_id}\n"
meebox iT邦新手 3 級 ‧ 2024-09-27 09:28:07 檢舉
你的問題是你輸出結果在日期、時間、UserID 之後都會有多的字嗎?
angel_111 iT邦新手 5 級 ‧ 2024-09-30 10:16:36 檢舉
是的,但問題已經處理好了。程式碼修改成以下這樣就不會列出多餘的資料:
date = line.split(" ")[0]
time = line.split(" ")[1]
user_id = line.split("UserID:")[1].split("\t")[0].strip()
print(f"Date: {date} | Time: {time} | UserID: {user_id} | Error: {keyword}")
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0

The Stickman Hook game’s physics engine is well-designed, making the swinging mechanics feel realistic and fun. The variety of obstacles ensures that the game doesn’t become repetitive, and there’s always something new to discover as you progress.

我要發表回答

立即登入回答