我已經難倒尋找一種方法來查找和替換基於位置的字符替換Python-。 基本上我在尋找什麼做進入的文檔和替換查找和基於未知字符
<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>
@Parfait
的正則表達式將解決這一和其它類似的問題,你應該繼續閱讀它們。在這種特定情況下,解析和格式化日期也是一種好方法。 –
我會進一步警告你不要試圖處理XML太多不使用庫,例如'lxml'或'ElementTree'實際上解析成一個適當的樹,雖然你可能會擺脫它,如果你所有的transormations如無併發症。 – holdenweb
它不能強調不夠(也許是最高的投票SO答案),[不要正則表達式HTML/XML文件(http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-自含標籤)。 – Parfait