如何使用python将.txt文件转换为xml文件?

如何使用python将.txt文件转换为xml文件?,第1张

如何使用python将.txt文件转换为xml文件?

尝试以下代码作为入门:

#!python3import reimport xml.etree.ElementTree as ETrex = re.compile(r'''(?P<title>Longitude |Latitude |date&time |gsms+cells+id          )          s*:?s*          (?P<value>.*)          ''', re.VERBOSE)root = ET.Element('root')root.text = 'n'    # newline before the celldata elementwith open('cell.txt') as f:    celldata = ET.SubElement(root, 'celldata')    celldata.text = 'n'    # newline before the collected element    celldata.tail = 'nn'  # 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 = 'nn'        # 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)

它显示您的示例数据:

<root><celldata><latitude>23.1100348</latitude><longitude>72.5364922</longitude><datetime>30:August:2014 05:04:31 PM</datetime><gsmcellid>4993</gsmcellid></celldata><celldata><latitude>23.1120549</latitude><longitude>72.5397988</longitude><datetime>30:August:2014 05:04:34 PM</datetime><gsmcellid>4993</gsmcellid></celldata></root>

所需近邻列表的更新:

#!python3import reimport xml.etree.ElementTree as ETrex = re.compile(r'''(?P<title>Longitude |Latitude |date&time |gsms+cells+id |Neighborings+List-s+Lacs+:s+Cids+:s+RSSI          )          s*:?s*          (?P<value>.*)          ''', re.VERBOSE)root = ET.Element('root')root.text = 'n'    # newline before the celldata elementwith open('cell.txt') as f:    celldata = ET.SubElement(root, 'celldata')    celldata.text = 'n'    # newline before the collected element    celldata.tail = 'nn'  # 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 = 'nn'        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)

更新以在邻居之前接受空行 -更好的通用实现:

#!python3import reimport xml.etree.ElementTree as ETrex = re.compile(r'''(?P<title>Longitude |Latitude |date&time |gsms+cells+id |Neighborings+List-s+Lacs+:s+Cids+:s+RSSI          )          s*:?s*          (?P<value>.*)          ''', re.VERBOSE)root = ET.Element('root')root.text = 'n'    # newline before the celldata elementwith open('cell.txt') as f:    celldata = ET.SubElement(root, 'celldata')    celldata.text = 'n'    # newline before the collected element    celldata.tail = 'nn'  # 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 = 'nn'     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 debuggingET.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
)。识别输入后,您将箭头(图论中的定向边)绘制到另一种状态(可能是同一状态,就像循环返回到同一节点一样)。箭头标有“条件|
行动’。

一开始的结果可能看起来很复杂;但是,从某种意义上说,您总是可以只专注于属于特定状态的代码部分,这很容易。而且,可以轻松修改代码。但是,有限自动机的功能有限。但是它们只是解决此类问题的理想之选。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5674801.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存