我很难找到找到并根据位置替换字符的方法。 基本上我要做的就是进入文档并替换
<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
答案 0 :(得分:0)
一种方式:
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本身就有很好的字符串操作。
答案 1 :(得分:0)
考虑XSLT(用于转换XML文档的专用声明性语言),它具有非常方便的功能(与其兄弟,XPath共享)以满足您的需求substring-before()
,您可以在其中提取数据到划定时间戳的时期。 Python的lxml
模块可以运行XSLT 1.0脚本。
下面的脚本从文件解析XML和XSLT。具体来说,XSLT运行Identity Transform以按原样复制文档,然后从所有 <gco:DateTime>
中提取时间。请注意,在XSLT标头中只定义了所需的gco
命名空间:
XSLT 脚本(在外部另存为.xsl文件,以便在Python中引用)
<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>