Issue
Using an InterShop XML file, I need to add the following tag inside a tag using Python and BeautifulSoup.
What I'd like is to write this :
valueNouveau = soup.new_tag('custom-attribute', {"name" : "ImagesWEB"},{"xml:lang" : "fr-FR"})
valueNouveau.string = 'chaine'
tagKeywords.append(valueNouveau)
when I do this it fails when writing the file with msg
unsupported operand type(s) for +: 'dict' and 'str' File
"/Users/cchauvin/Documents/Synonymes/Synoymes_etcetera.py", line 116,
in main f_output.write(str(soup))
When I write this, everything runs smoothly and my file is written
valueNouveau = soup.new_tag('custom-Chauvin')
valueNouveau.string ='chaine'
tagKeywords.append(valueNouveau)
What's wrong ? I need to add this tag in the existing xml file
<custom-attribute name="ImagesWEB" dt:dt="string" xml:lang="fr-FR">
Many thanks in advance
Solution
You need not pass every attribute of a tag in unique dictionaries, just create a single dictionary and put all the attributes you want inside it and pass it into the method. Like this:
dict_attributes = {"name" : "ImagesWEB",
"xml:lang" : "fr-FR"}
valueNouveau = soup.new_tag('custom-attribute',attrs=dict_attributes)
valueNouveau.string = 'chaine'
tagKeywords.append(valueNouveau)
Output:
<custom-attribute name="ImagesWEB" xml:lang="fr-FR">chaine</custom-attribute>
Answered By - Ananth
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.