我有以下xml格式的数据。
<UserStaging>
<Staging>
<Stage Type="Stage1" Start="0" />
<Stage Type="Stage2" Start="50"/>
<Stage Type="Stage3" Start="100" />
</Staging>
</UserStaging>
我必须以这种格式获取它:
<epoch><epoch_start>0</epoch_start<epoch_end>50</epoch_end><stage>NREM1</stage></epoch>
<epoch><epoch_start>50</epoch_start<epoch_end>100</epoch_end<stage>NREM2</stage></epoch>
<epoch><epoch_start>100</epoch_start<epoch_end>9999</epoch_end<stage>NREM3</stage></epoch>
其中Stage1是NREM1,Stage2是NREM2,依此类推,一个项目的“ epoch_end”是下一个项目的“开始”。 时期时间是可变的。
我将如何使用python解析xml数据? 还有什么比使用minidump更好的方法了吗? 适当的解析命令是什么样的? 感谢您的答复。
答案 0 :(得分:0)
用于这种工作的常用工具是XSLT。在这种情况下,可以使用XSLT 1.0轻松完成此工作,而XSLT 1.0可轻松用于Python:
<xsl:stylesheet...
<xsl:template match="Stage">
<epoch>
<epoch_start>
<xsl:value-of select="@Start"/>
</epoch_start>
<epoch_end>
<xsl:variable name="next" select="following-sibling::*[1]"/>
<xsl:choose>
<xsl:when test="$next">
<xsl:value-of select="$next/@Start"/>
<xsl:when>
<xsl:otherwise>9999</xsl:otherwise>
</xsl:choose>
</epoch_end>
<stage>NREM<xsl:number/></stage>
</epoch>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
基于xml.etree.ElementTree的解决方案。可以在没有用于调试的字典中间列表的情况下实现。
HoughLinesP(edges, linesP, 1, CV_PI/180, 50, 50, 10 );
printf("Probabilistic Hough found %ld lines\n",linesP.size());
// Draw the lines extracted
cvtColor(edges, coloredges, CV_GRAY2BGR);
vector<Vec2f> VlinesP;
for( size_t i = 0; i < linesP.size(); i++ )
{
Vec4i l = linesP[i];
line( coloredges, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 1, CV_AA);
}
imshow("Probabilistic Hough detected lines", coloredges);
waitKey(0);
return 0;
}
输出:
import xml.etree.ElementTree as ET
data = '''<UserStaging>
<Staging>
<Stage Type="Stage1" Start="0" />
<Stage Type="Stage2" Start="50"/>
<Stage Type="Stage3" Start="100" />
</Staging>
</UserStaging>'''
tree = ET.fromstring(data)
new_data = []
start = 0
for idx, stage in enumerate(tree.iter('Stage')):
new_data.append({'start': stage.attrib['Start'],
'stage': 'NREM{}'.format(idx + 1)})
if idx > 0:
new_data[idx - 1]['end'] = stage.attrib['Start']
root = ET.Element("UserStaging")
for idx, entry in enumerate(new_data):
epoch = ET.SubElement(root, "epoch")
start = ET.SubElement(epoch, "epoch_start").text = entry['start']
end = ET.SubElement(epoch, "epoch_end").text = entry['end'] if idx < len(new_data) - 1 else '9999'
stage = ET.SubElement(epoch, "stage").text = entry['stage']
ET.dump(root)