You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However if tag a has parameter, its value must be a string, which is unexpected due to previous example.
data= {"a": {"@param": "test", "#text": 1}}
Throws an exception TypeError: decoding to str: need a bytes-like object, int found.
Param is integer, value is string
This works fine as well as simple case when only value of tag is provided:
data= {"a": {"@param": 1, "#text": "test"}}
<aparam="1">test</a>
The text was updated successfully, but these errors were encountered:
Behoston
changed the title
Tag value (#text) must be a string when tag has additional parameters
Tag value (#text) must be a string when tag has additional parameters - unparse
Dec 9, 2024
And will always throw with ints, floats or decimals with usual encoding like utf-8, latin-1, ascii etc.
What might help is if xmltodict pre-encoded the data beforehand in the _emit() function: xmltodict.py:486
ifcdataisnotNone:
# BEGIN PATCHifnotisinstance(cdata, str):
cdata=str(cdata).encode(encoding, 'ignore').decode(encoding)
# END PATCHcontent_handler.characters(cdata)
And would require the _emit function to accept an encoding parameter, passed in from unparse()
But I haven't thought deeply about this solution and the maintainer of this project would be better suited to understand the hazards of this approach.
In the meanwhile we'll have to preprocess data with numeric #text values.
Simple case - no params
works as expected, producing:
One param
However if tag
a
has parameter, its value must be a string, which is unexpected due to previous example.Throws an exception
TypeError: decoding to str: need a bytes-like object, int found
.Param is integer, value is string
This works fine as well as simple case when only value of tag is provided:
The text was updated successfully, but these errors were encountered: