I have a textfile (actually, .xml) that contains certain lines of the form MACAddress=SOMEVALUE, and mixed within other lines. For example, a typical such line is (on a single line):
<Adapter slot="3" enabled="false" MACAddress="080027671C79" cable="true" speed="0"
type="Am79C973">
N.B. In the line above, SOMEVALUE is "080027671C79" precisely.
So, I would want to parse the fi开发者_Go百科le, and collect all of the SOMEVALUES. How can I do this in python (re ?, find?) ?
Thanks in advance.
It's XML, so use an XML parser. For example:
from xml.dom.minidom import parse
macs = [adapter.getAttribute("MACAddress") for adapter in
parse("file.xml").getElementsByTagName("Adapter")]
Not sure that i correctly know what is MAC address but please fine two more options below:
Input:
text = """
Adapter slot="3" enabled="false" Mcable="true" speed="0" type="Am79C973">
Adapter slot="3" enabled="false" MACAddress="080027671C79" cable="true" speed="0" type="Am79C973">
Adapter slot="3" enabled="false" MACAddress="080027671C71" cable="true" speed="2" type="Am79C973">
Adapter slot="3" enabled="false" MACAddress="080024671C79" cable="true" speed="1" type="Am79C973">
Adapter slot="3" enabled="false" MACAddress="080227671C79" cable="true" speed="1" type="Am79C973">
"""
1) RegEx:
from re import findall
findall(r'(?i)(?<=MACAddress=\")\w{12}', text)
2) String slicing:
searchTxt = 'MACAddress="'
searchLen = len(searchTxt)
getMAC = lambda line: line[line.index(searchTxt) + searchLen: line.index(searchTxt) + searchLen + 12]
map(lambda line: getMAC(line), (line for line in text.split() if searchTxt in line))
A regex will be the faster tool.
The one of Artsiom is too restrictive, I think. I would do:
from re import findall
findall('MACAddress=([^ \t]+)', text)
But I think you want what is BETWEEN the quotes " . Then:
from re import findall
findall('MACAddress="([^"]+)"', text)
.
Is it possible that the SOMEVALUE you want to catch contain quotes " ?
精彩评论