I'm studying an elementary course in programming in python and I have a question.
I have a string in python which includes math information about points, lines and segments.
The string contains a lot of information that has to be ignored. But whenever the words "point","line" or "segment" are found, I need to extract the coordinates from such elements and copy them into a list.
I need three different lists. One to store co-ords from points, a second one for lines and a third one for segments.
Since the string is too long, I'll only paste an example of every type of element (I have excluded non relevant parts of the string):
<element type="point" label="D">
<show object="true" label="true"/>
<objColor r="0" g="0" b="255" alpha="0.0"/>
<layer val="0"/>
<labelMode val="0"/>
<animation step="1" speed="1" type="1" playing="false"/>
<coords x="6.14" y="3.44" z="1.0"/>
<pointSize val="3"/>
<pointStyle val="0"/>
</element>
<element type="segment" label="a">
<show object="true" label="true"/>
<objColor r="153" g="51" b="0" alpha="0.0"/>
<layer val="0"/>
<labelMode val="0"/>
<auxiliary val="false"/>
<coords x="2.68" y="3.44" z="-12.0192"/>
<lineStyle thickness="2" type="0" typeHidden="1"/>
<outlyingIntersections val="false"/>
<keepTypeOnTransform val="true"/>
</element>
<element type="line" label="b">
<show object="true" label="true"/>
<objColor r="0" g="0" b="0" alpha="0.0"/>
<layer val="0"/>
<labelMode val="0"/>
<coords x="-1.3563823178988361" y="3.7135968534106922" z="-13.20117532177342"/>
<lineStyle thickness="2" type="0" typeHidden="1"/>
<eqnStyle style="implicit"/>
</element>
Is there any easy way to do what I want? Thanks in advance
Have a look at BeautifulSoup. It allows you to get elements by their IDs or tags. It is very useful for basic XML parsing.
You can just call beutiful soup with the XML string and then you can call the BeautifulSoup methods
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With