碰巧,这里包括我的问题的解决方案。示例文件包括在内。
代码:
fp = r'PATH_TO_FILE'
aliases = []
aliases.append(("sitex","sitez","sitey"))
splitFile = []
for l in open(fp):
parts = tuple(l[:-1].replace(" ","").split(","))
splitFile.append(parts)
def isAlias(old, new):
print old, new
aliasFound = False
for alias in aliases:
if old in alias and new in alias:
aliasFound = True
return aliasFound
handledSites = []
for split in splitFile:
log = split[0]
site = split[1]
rp = split[2]
matchFound = False
for hs in handledSites:
if site in hs[0]:
matchFound = True
if rp not in hs[1]:
hs[1].append(rp)
if log not in hs[2]:
hs[2].append(log)
if not matchFound:
if isAlias(hs[0][0], site):
matchFound = True
hs[0].append(site)
if rp not in hs[1]:
hs[1].append(rp)
if log not in hs[2]:
hs[2].append(log)
if not matchFound:
handledSites.append(([site],[rp],[log]))
for s in handledSites:
print s
示例文件
logfile[date]_[server]_sitex.log, sitex, rp1
logfile[date]_[server]_sitex.log, sitex, rp2
logfile[date]_[server]_sitey.log, sitey, rp1
logfile[date]_[server]_sitey.log, sitey, rp2
logfile[date]_[server]_sitez.log, sitez, rp1
logfile[date]_[server]_sitez.log, sitez, rp2
logfile[date]_[server]_site3.log, site3, rp1
logfile[date]_[server]_site3.log, site3, rp2