[英]Python regular expression to get filename in a long path
我需要分析一些日志文件,如下所示,我想檢索 3 部分數據,
我使用這個正則表達式,但它無法獲得第三部分。
(\d\d:\d\d:\d\d).*(ABC|DEF).*\\(\d\w\.?\w\..*)\soutput.*
任何建議將不勝感激。
08:38:36 TestModule - [INFO]result success !! ftp_site=ftp.test.com file_dir=CPY input file=\root\level1\level2-ABC\2C.013000000B.dat output file=c:\local\project1\data\2C.013000000B.dat.ext
06:40:37 TestModule - [INFO]result success !! ftp_site=ftp.test.com file_dir=CPY input file=\root\level1\level2-ABC\20100722B.TXT output file=c:\local\project1\data\20100722B.TXT.ext
06:40:39 TestModule - [INFO]result success !! ftp_site=ftp.test.com file_dir=CPY input file=\root\level1\level2-DEF\20100722D1-XYZ.TXT output file=c:\local\project1\data\20100722D1-YFP.TXT.ext
06:40:42 TestModule - [INFO]result success !! ftp_site=ftp.test.com file_dir=CPY input file=\root\level1\level2-DEF\2C.250B output file=c:\local\project1\data\2C.250B.ext
BR
愛德華
正則表達式非常擅長解決這樣的問題——即解析日志文件記錄。 MarcoS 的回答很好地解決了您的直接問題。 但是,另一種方法是編寫一個(可重用的)通用 function,它將日志文件記錄分解為其各種組件並返回包含所有這些已解析組件的匹配 object。 分解后,可以輕松地將測試應用於組件部分以檢查各種要求(例如輸入文件路徑必須以ABC
或DEF
結尾)。 這是一個 python 腳本,它有一個 function: decomposeLogEntry()
並演示如何使用它來解決您手頭的問題:
import re
def decomposeLogEntry(text):
r""" Decompose log file entry into its various components.
If text is a valid log entry, return regex match object of
log entry components strings. Otherwise return None."""
return re.match(r"""
# Decompose log file entry into its various components.
^ # Anchor to start of string
(?P<time>\d\d:\d\d:\d\d) # Capture: time
\s+
(?P<modname>\w+?) # Capture module name
\s-\s\[
(?P<msgtype>[^]]+) # Capture message type
\]
(?P<message>[^!]+) # Capture message text
!!\sftp_site=
(?P<ftpsite>\S+?) # Capture ftp URL
\sfile_dir=
(?P<filedir>\S+?) # Capture file directory?
\sinput\sfile=
(?P<infile> # Capture input path and filename
(?P<infilepath>\S+)\\ # Capture input file path
(?P<infilename>[^\s\\]+) # Capture input file filename
)
\soutput\sfile=
(?P<outfile> # Capture input path and filename
(?P<outfilepath>\S+)\\ # Capture output file path
(?P<outfilename>[^\s\\]+) # Capture output file filename
)
\s* # Optional whitespace at end.
$ # Anchor to end of string
""", text, re.IGNORECASE | re.VERBOSE)
# Demonstrate decomposeLogEntry function. Print components of all log entries.
f=open("testdata.log")
mcnt = 0
for line in f:
# Decompose this line into its components.
m = decomposeLogEntry(line)
if m:
mcnt += 1
print "Match number %d" % (mcnt)
print " Time: %s" % m.group("time")
print " Module name: %s" % m.group("modname")
print " Message type: %s" % m.group("time")
print " Message: %s" % m.group("message")
print " FTP site URL: %s" % m.group("ftpsite")
print " Input file: %s" % m.group("infile")
print " Input file path: %s" % m.group("infilepath")
print " Input file name: %s" % m.group("infilename")
print " Output file: %s" % m.group("outfile")
print " Output file path: %s" % m.group("outfilepath")
print " Output file name: %s" % m.group("outfilename")
print "\n",
f.close()
# Next pick out only the desired data.
f=open("testdata.log")
mcnt = 0
matches = []
for line in f:
# Decompose this line into its components.
m = decomposeLogEntry(line)
if m:
# See if this record meets desired requirements
if re.search(r"ABC$|DEF$", m.group("infilepath")):
matches.append(line)
f.close()
print "There were %d matching records" % len(matches)
這個 function 不僅可以挑選出您感興趣的各個部分,還可以驗證輸入並拒絕格式錯誤的記錄。 一旦編寫和調試,這個 function 可以被其他需要分析日志文件以滿足其他要求的程序重用。
這是應用到您的測試數據時腳本中的 output:
r"""
Match number 1
Time: 08:38:36
Module name: TestModule
Message type: 08:38:36
Message: result success
FTP site URL: ftp.test.com
Input file: \root\level1\level2-ABC\2C.013000000B.dat
Input file path: \root\level1\level2-ABC
Input file name: 2C.013000000B.dat
Output file: c:\local\project1\data\2C.013000000B.dat.ext
Output file path: c:\local\project1\data
Output file name: 2C.013000000B.dat.ext
Match number 2
Time: 06:40:37
Module name: TestModule
Message type: 06:40:37
Message: result success
FTP site URL: ftp.test.com
Input file: \root\level1\level2-ABC\20100722B.TXT
Input file path: \root\level1\level2-ABC
Input file name: 20100722B.TXT
Output file: c:\local\project1\data\20100722B.TXT.ext
Output file path: c:\local\project1\data
Output file name: 20100722B.TXT.ext
Match number 3
Time: 06:40:39
Module name: TestModule
Message type: 06:40:39
Message: result success
FTP site URL: ftp.test.com
Input file: \root\level1\level2-DEF\20100722D1-XYZ.TXT
Input file path: \root\level1\level2-DEF
Input file name: 20100722D1-XYZ.TXT
Output file: c:\local\project1\data\20100722D1-YFP.TXT.ext
Output file path: c:\local\project1\data
Output file name: 20100722D1-YFP.TXT.ext
Match number 4
Time: 06:40:42
Module name: TestModule
Message type: 06:40:42
Message: result success
FTP site URL: ftp.test.com
Input file: \root\level1\level2-DEF\2C.250B
Input file path: \root\level1\level2-DEF
Input file name: 2C.250B
Output file: c:\local\project1\data\2C.250B.ext
Output file path: c:\local\project1\data
Output file name: 2C.250B.ext
There were 4 matching records
"""
如果您使用 regex 工具,它將使您的 regex 故障排除工作變得更加輕松。 試試這個免費的- 可能有更好的,但這很好用。 您可以將您的日志文件粘貼到那里,並一次嘗試一下您的正則表達式,它會實時突出顯示匹配項。
為什么是正則表達式?
考慮使用split
來獲取所有單詞。 這將直接為您提供時間戳。 然后 go 通過所有其他詞,檢查其中是否有=
,在這種情況下再次拆分它們,您就有了路徑和其他參數。 標准 Python 路徑處理 ( os.path
) 將幫助您獲取文件夾和文件名。
當然,如果您的路徑名可能包含空格,則此方法會失敗,但否則絕對值得考慮。
您可以通過正常的字符串處理簡單地做到這一點
f=open("file")
for line in f:
date,b = line.split("input")
print "time: " , date.split()[0]
input_path = b.split("output")[0]
tokens=input_path.split("\\")
filename=tokens[-1]
directory=tokens[-2].split("-")[-1]
print filename, directory
f.close()
這適用於您的示例:
r'(\d\d:\d\d:\d\d).*(ABC|DEF).*?([^\\]*)\soutput.*'
盡管寫得很好的正則表達式在這里是合適的,但我會以不同的方式處理這個問題。 更具體地說, os.path.split
旨在將文件名與基本路徑分開,並處理此正則表達式忽略的所有極端情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.