尝试以下代码作为启动程序:#!python3
import re
import xml.etree.ElementTree as ET
rex = re.compile(r'''(?P
Longitude|Latitude
|date&time
|gsm\s+cell\s+id
)
\s*:?\s*
(?P.*)
''', re.VERBOSE)
root = ET.Element('root')
root.text = '\n' # newline before the celldata element
with open('cell.txt') as f:
celldata = ET.SubElement(root, 'celldata')
celldata.text = '\n' # newline before the collected element
celldata.tail = '\n\n' # empty line after the celldata element
for line in f:
# Empty line starts new celldata element (hack style, uggly)
if line.isspace():
celldata = ET.SubElement(root, 'celldata')
celldata.text = '\n'
celldata.tail = '\n\n'
# If the line contains the wanted data, process it.
m = rex.search(line)
if m:
# Fix some problems with the title as it will be used
# as the tag name.
title = m.group('title')
title = title.replace('&', '')
title = title.replace(' ', '')
e = ET.SubElement(celldata, title.lower())
e.text = m.group('value')
e.tail = '\n'
# Display for debugging
ET.dump(root)
# Include the root element to the tree and write the tree
# to the file.
tree = ET.ElementTree(root)
tree.write('cell.xml', encoding='utf-8', xml_declaration=True)
它显示示例数据:
23.1100348
72.5364922
30:August:2014 05:04:31 PM
4993
23.1120549
72.5397988
30:August:2014 05:04:34 PM
4993
更新想要的邻居列表:#!python3
import re
import xml.etree.ElementTree as ET
rex = re.compile(r'''(?P
Longitude|Latitude
|date&time
|gsm\s+cell\s+id
|Neighboring\s+List-\s+Lac\s+:\s+Cid\s+:\s+RSSI
)
\s*:?\s*
(?P.*)
''', re.VERBOSE)
root = ET.Element('root')
root.text = '\n' # newline before the celldata element
with open('cell.txt') as f:
celldata = ET.SubElement(root, 'celldata')
celldata.text = '\n' # newline before the collected element
celldata.tail = '\n\n' # empty line after the celldata element
for line in f:
# Empty line starts new celldata element (hack style, uggly)
if line.isspace():
celldata = ET.SubElement(root, 'celldata')
celldata.text = '\n'
celldata.tail = '\n\n'
else:
# If the line contains the wanted data, process it.
m = rex.search(line)
if m:
# Fix some problems with the title as it will be used
# as the tag name.
title = m.group('title')
title = title.replace('&', '')
title = title.replace(' ', '')
if line.startswith('Neighboring'):
neighbours = ET.SubElement(celldata, 'neighbours')
neighbours.text = '\n'
neighbours.tail = '\n'
else:
e = ET.SubElement(celldata, title.lower())
e.text = m.group('value')
e.tail = '\n'
else:
# This is the neighbour item. Split it by colon,
# and set the attributes of the item element.
item = ET.SubElement(neighbours, 'item')
item.tail = '\n'
lac, cid, rssi = (a.strip() for a in line.split(':'))
item.attrib['lac'] = lac
item.attrib['cid'] = cid
item.attrib['rssi'] = rssi.split()[0] # dBm removed
# Include the root element to the tree and write the tree
# to the file.
tree = ET.ElementTree(root)
tree.write('cell.xml', encoding='utf-8', xml_declaration=True)
在邻居之前接受空行的更新--也可以更好地实现一般目的:#!python3
import re
import xml.etree.ElementTree as ET
rex = re.compile(r'''(?P
Longitude|Latitude
|date&time
|gsm\s+cell\s+id
|Neighboring\s+List-\s+Lac\s+:\s+Cid\s+:\s+RSSI
)
\s*:?\s*
(?P.*)
''', re.VERBOSE)
root = ET.Element('root')
root.text = '\n' # newline before the celldata element
with open('cell.txt') as f:
celldata = ET.SubElement(root, 'celldata')
celldata.text = '\n' # newline before the collected element
celldata.tail = '\n\n' # empty line after the celldata element
status = 0 # init status of the finite automaton
for line in f:
if status == 0: # lines of the heading expected
# If the line contains the wanted data, process it.
m = rex.search(line)
if m:
# Fix some problems with the title as it will be used
# as the tag name.
title = m.group('title')
title = title.replace('&', '')
title = title.replace(' ', '')
if line.startswith('Neighboring'):
neighbours = ET.SubElement(celldata, 'neighbours')
neighbours.text = '\n'
neighbours.tail = '\n'
status = 1 # empty line and then list of neighbours expected
else:
e = ET.SubElement(celldata, title.lower())
e.text = m.group('value')
e.tail = '\n'
# keep the same status
elif status == 1: # empty line expected
if line.isspace():
status = 2 # list of neighbours must follow
else:
raise RuntimeError('Empty line expected. (status == {})'.format(status))
status = 999 # error status
elif status == 2: # neighbour or the empty line as final separator
if line.isspace():
celldata = ET.SubElement(root, 'celldata')
celldata.text = '\n'
celldata.tail = '\n\n'
status = 0 # go to the initial status
else:
# This is the neighbour item. Split it by colon,
# and set the attributes of the item element.
item = ET.SubElement(neighbours, 'item')
item.tail = '\n'
lac, cid, rssi = (a.strip() for a in line.split(':'))
item.attrib['lac'] = lac
item.attrib['cid'] = cid
item.attrib['rssi'] = rssi.split()[0] # dBm removed
# keep the same status
elif status == 999: # error status -- break the loop
break
else:
raise LogicError('Unexpected status {}.'.format(status))
break
# Display for debugging
ET.dump(root)
# Include the root element to the tree and write the tree
# to the file.
tree = ET.ElementTree(root)
tree.write('cell.xml', encoding='utf-8', xml_declaration=True)
代码实现了所谓的有限自动机,其中status变量表示其当前状态。你可以用铅笔和纸把它形象化——画一个小圆圈,里面有状态号(图论中称为节点)。处于这种状态时,只允许某种输入(line)。当输入被识别时,您将箭头(图论中的定向边)绘制到另一个状态(可能是相同的状态,作为返回到相同节点的循环)。箭头被注释为“condition | action”。
结果在一开始可能看起来很复杂;但是,从某种意义上说,您可以始终将精力集中在属于特定状态的代码部分上,这是很容易的。而且,代码很容易修改。然而,有限自动机的能力有限。但对于这类问题来说,它们是完美的。