研究多行Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18521319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
re.search Multiple lines Python
提问by Xariec
re.search with \s or '\n' is not finding the multiline i'm trying to search for.
使用 \s 或 '\n' 进行 re.search 没有找到我要搜索的多行。
Portion of Source:
部分来源:
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 -----
Looking at it in Notepad++ I see "BUILDING DATA:(LF)"
在 Notepad++ 中查看它,我看到“构建数据:(LF)”
Full Code
完整代码
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.
但我没有得到任何回报。
I need to put an empty spacer in when "DEFENSE GRID" doesn't exist after "BUILDING DATA:"
当“构建数据:”之后不存在“防御网格”时,我需要放入一个空的间隔器
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.search,但我找不到任何解释多行如何工作的详尽示例。
回答by Saullo G. P. Castro
You can do just what you did, but using re.findall
instead of re.search
:
你可以做你所做的,但使用re.findall
而不是re.search
:
re.findall('BUILDING DATA:\nN/A', a, re.M)
#['BUILDING DATA:\nN/A']
EDIT:
编辑:
The problem is that you are currently reading line-by-line. In order to detect a pattern that belongs to two or more lines, you have to consider the string as a whole, maybe doing:
问题是您目前正在逐行阅读。为了检测属于两行或更多行的模式,您必须将字符串视为一个整体,可能会这样做:
s = ''.join(lines)
which is ok if lines
is not so big, and then use s
to perform your multi-line searches...
如果lines
不是那么大就可以了,然后用于s
执行多行搜索...
回答by Goontracker
re.findall("BUILDING DATA:\nN/A",a,re.MULTILINE)
回答by Stefan Bollmann
I wonder why you have nothing returned. If your file looks like this:
我想知道为什么你什么都没有回来。如果您的文件如下所示:
BUILDING DATA:
N/A
I get using
我开始使用
import re
f = open('test.txt','r')
a = f.read(20)
re.search('BUILDING DATA:\nN/A', a, re.M)
an output. This is
一个输出。这是
<_sre.SRE_Match object at 0x1004fc8b8>
If I test re.search with string, that is not in the file like in this code:
如果我用字符串测试 re.search,它不会像下面的代码那样在文件中:
import re
f = open('test.txt','r')
a = f.read(20)
re.search('BUILDING BATA:\nN/A', a, re.M)
there is no output as expected.
没有预期的输出。
EDIT:
编辑:
As Saullo Castro pointed out, the problem is the line-by-line reading. Why not use something like this?
正如 Saullo Castro 所指出的,问题在于逐行阅读。为什么不使用这样的东西?
a = open('scan.txt','r').read()
if re.findall('BUILDING DATA:\nN/A', a, re.M):
print('found!')
3rd try:
第三次尝试:
tmp = False
...
elif re.findall('BUILDING DATA:', a, re.M):
tmp = True
elif tmp and re.findall('N/A', a, re.M):
def_grid = ''
print "Didn't find it"
defense.append(def_grid)
defense_count +=1
回答by sharhp
Replace
代替
re.findall('BUILDING DATA:\sN/A', a, re.M):
with
和
re.findall('BUILDING DATA:\nN/A', a, re.M):
or
或者
re.search(r'BUILDING DATA:\nN/A', a, re.M):
and it should work.
它应该工作。
(Notice that in your code there's \s
instead of \n
)
(请注意,在您的代码中有\s
而不是\n
)