我已经难倒寻找一种方法来查找和替换基于位置的字符替换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