簡體   English   中英

在Python中查找和替換-基於未知字符

[英]Find and Replace in Python- based on unknown characters

我一直很想尋找一種基於位置來查找和替換字符的方法。 基本上我想要做的是進入文檔並替換

<gco:DateTime>2016-04-20T11:27:34.8677919-06:00</gco:DateTime>

<gco:DateTime>2016-04-20T11:27:34</gco:DateTime>

小數點后的所有內容都必須刪除 問題是,這是針對XML文件中的多個時間戳,並且每個時間戳都是完全不同的。 我已經閱讀了一些有關正則表達式的內容,這似乎是一種可能的方法。 任何幫助將不勝感激。

XML文件格式的編輯示例:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='http://ngis/ngis/metadata/StyleSheet/xslt/nGIS_Metadata.xslt'?>
<gmd:MD_Metadata xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:gmx="http://www.isotc211.org/2005/gmx" xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:gfc="http://www.isotc211.org/2005/gfc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gss="http://www.isotc211.org/2005/gss" xmlns:gsr="http://www.isotc211.org/2005/gsr" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gmi="http://www.isotc211.org/2005/gmi" xmlns:gmd="http://www.isotc211.org/2005/gmd">
    <gmd:fileIdentifier>
        <gco:CharacterString>BF244A7CB62491BC74B001BE5DEAA213AAFB9DBA</gco:CharacterString>
    </gmd:fileIdentifier>
    <gmd:language>
        <gco:CharacterString>English</gco:CharacterString>
                <gmd:date>
                <gco:DateTime>2016-04-20T11:27:34.8677919-06:00</gco:DateTime>
                </gmd:date>

@芭菲

單程:

s = "<gco:DateTime>2016-04-20T11:27:34.8677919-06:00</gco:DateTime>"
split_on_dot = s.split('.')
split_on_angle = split_on_dot[1].split('<')
new_s = "".join([split_on_dot[0], "<", split_on_angle[1]])

>>> new_s
'<gco:DateTime>2016-04-20T11:27:34</gco:DateTime>'
>>> 

這取決於句點是輸入字符串中的唯一句點。 我不太擅長正則表達式。 我認為它們被過度使用了,但是我敢肯定有人會向您展示如何使用正則表達式。 只要記住python本身具有良好的字符串操作即可。

考慮使用XSLT (一種用於轉換XML文檔的專用聲明性語言),它具有非常方便的功能(與它的同級XPath共享),可以滿足您的需要substring-before() ,在此之前您可以在划分時間戳記的時間段之前提取數據。 Python的lxml模塊可以運行XSLT 1.0腳本。

下面的腳本從文件中解析XML和XSLT。 具體來說,XSLT運行Identity Transform <gco:DateTime>復制文檔,然后從所有 <gco:DateTime>提取時間。 請注意,在XSLT標頭中僅定義了所需的gco命名空間:

XSLT腳本(另存為要在Python中引用的.xsl文件)

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
               xmlns:gco="http://www.isotc211.org/2005/gco">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>

  <!-- Identity Transform -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="gco:DateTime">
    <xsl:copy>
      <xsl:copy-of select="substring-before(., '.')"/>                  
    </xsl:copy>
  </xsl:template>

</xsl:transform>

Python腳本

import lxml.etree as ET

# LOAD XML AND XSL
dom = ET.parse('Input.xml')
xslt = ET.parse('XSLTScript.xsl')

# TRANSFORM XML 
transform = ET.XSLT(xslt)
newdom = transform(dom)

# CONVERT TO STRING
tree_out = ET.tostring(newdom, encoding='UTF-8', pretty_print=True, xml_declaration=True)

# OUTPUT TREE TO FILE
xmlfile = open('Output.xml')
xmlfile.write(tree_out)
xmlfile.close()

輸出量

<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='http://ngis/ngis/metadata/StyleSheet/xslt/nGIS_Metadata.xslt'?><gmd:MD_Metadata xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:gmx="http://www.isotc211.org/2005/gmx" xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:gfc="http://www.isotc211.org/2005/gfc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gss="http://www.isotc211.org/2005/gss" xmlns:gsr="http://www.isotc211.org/2005/gsr" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gmi="http://www.isotc211.org/2005/gmi" xmlns:gmd="http://www.isotc211.org/2005/gmd">
  <gmd:fileIdentifier>
    <gco:CharacterString>BF244A7CB62491BC74B001BE5DEAA213AAFB9DBA</gco:CharacterString>
  </gmd:fileIdentifier>
  <gmd:language>
    <gco:CharacterString>English</gco:CharacterString>
    <gmd:date>
      <gco:DateTime>2016-04-20T11:27:34</gco:DateTime>
    </gmd:date>
  </gmd:language>
</gmd:MD_Metadata>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM