'Getting the content of any specific tag anywhere in an XML file with Python
I have an XML file like this:
<file>
<dict>
<element>
<s>text</s>
<i>56</i>
<s>foobar</s> <--- strange formatting issues cause it to be below the root tag
... more tags like these
</element>
...more <element>s like above
</dict>
</file>
How can I get the content between all <s> and </s> tags in the entire file, no matter where the tags are? I'm running Python 3.10.4 and I prefer using the builtin xml module. (If I need to use a different one, I'd be OK with that!)
(Also, assume I don't want to fix the <s>foobar<\s> issue.)
Output I want:
text
foobar
...more content from between <s></s> tags
Solution 1:[1]
There is no easy answer, you'll have to either implement operations for 256 bits integers (still not going to help your division, where you should use a scaling factor to represent your numbers), implement operations for a fixed point representation, or operations for a floating point representation (mantissa + exponent), none of which are trivial endeavors.
For a 256 bits arithmetic, someone had created a proof of concept (but not battle tested): https://github.com/KStasi/clarity-uint256-lib. She was using 4 uint64 to be able to use uint128 arithmetic without overflows.
Here's a 16 bits multiplication using an 8 bit multiplier:https://www.techiedelight.com/multiply-16-bit-integers-using-8-bit-multiplier/. You'll need to do similar things with 128 bit numbers.
Similarly, here are some pointers for doing a division with lower precision arithmetic: http://www.mattmillman.com/mcs-48-the-quest-for-16-bit-division-on-the-8-bit-cpu-which-cant-divide-anything/
Instead of using a scaling factor, an other approach would be to use fixed point representation. See https://en.wikipedia.org/wiki/Fixed-point_arithmetic for theory, and https://schaumont.dyn.wpi.edu/ece4703b20/lecture6.html for more practical considerations (some DSPs have the same issue, they only have integer operations).
Solution 2:[2]
The problem here is that the scientific notations are not the same for both the operations. For example,2.5/4 = 6.25E16 * 10^-1 (scaled up)2.5*4 = 100 * 10^-1 (scaled down)
Here you can simply just add -16 to the exponent part if you are multiplying 1e16 in the mantissa. So your answer becomes
2.5/4 = 6.25E16 * 10^-1 (scaled up)2.5/4 = 6.25E16 * 10^-1 * 10^-162.5/4 = 6.25E16 * 10^-172.5/4 = 0.625
With this, both your multiplication and division are in scientific notations and result in the actual numbers.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pascal Belloncle |
| Solution 2 | Dharman |
