研究多行Python [英] re.search Multiple lines Python
本文介绍了研究多行Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
re.search with \s 或 '\n' 没有找到我要搜索的多行.
re.search with \s or '\n' is not finding the multiline i'm trying to search for.
部分来源:
Date/Time:
2013-08-27 17:05:36
----- BEGIN SEARCH -----
GENERAL DATA:
NAME: AB12
SECTOR:
999,999
CONTROLLED BY: Player
ALLIANCE: Aliance
ONLINE: 1 seconds ago
SIZE: Large
HOMEWORLD: NO
APPROVAL RATING: 100%
PRODUCTION RATE: 100%
RESOURCE DATA:
POWER: 0 / 0
BUILDINGS: 0 / 20
ORE: 80,000 / 80,000
CRYSTAL: 80,000 / 80,000
POPULATION: 40,000 / 40,000
BUILDING DATA:
N/A
UNIT DATA:
WYVERN(S): 100
----- END SEARCH -----
在 Notepad++ 中查看它,我看到BUILDING DATA:(LF)"
Looking at it in Notepad++ I see "BUILDING DATA:(LF)"
完整代码
lines = open('scan.txt','r').readlines()
for a in lines:
if re.search(r"\A\d", a):
digits = a
if re.search(r"2013", digits):
date.append(digits[:19])
count +=1
elif re.search(r",", digits):
clean = digits.rstrip()
sector = clean.split(',')
x.append(sector[0])
y.append(sector[1])
elif re.search(r"CONTROLLED BY:", a):
player.append(a[15:].rstrip())
elif re.search(r"ALLIANCE:", a):
alliance.append(a[10:].rstrip())
elif re.search(r"SIZE:", a):
size.append(a[6:].rstrip())
elif re.findall('BUILDING DATA:\sN/A', a, re.M):
def_grid = ''
print "Didn't find it"
defense.append(def_grid)
defense_count +=1
elif re.search(r"DEFENSE GRID", a):
def_grid = a[16:].rstrip()
print "defense found"
defense_count +=1
但我没有退回任何东西.
But I am not having anything returned.
当BUILDING DATA:"后不存在DEFENSE GRID"时,我需要放入一个空的间隔器
I need to put an empty spacer in when "DEFENSE GRID" doesn't exist after "BUILDING DATA:"
我知道我遗漏了一些东西,我已经尝试阅读 re.search,但我找不到任何详细的例子来解释多行是如何工作的.
I know i'm missing something and I've tried reading up on re.search but i'm not able to find any thorough examples that explain how the multiline works.
推荐答案
re.findall("BUILDING DATA:\nN/A",a,re.MULTILINE)
这篇关于研究多行Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文