Rewrite the name of the namespaced tag with lxml

from lxml import etree

class ElementsTagReplacer:
    """replace tag name with namespaces."""
    def __init__(self):
        self.encoding = 'utf-8'
        self.parser = etree.XMLParser(encoding=self.encoding)
        self.namespaces = {'media': "http://search.yahoo.com/mrss/"}

    def replace(self, xml):
        if isinstance(xml, str):
            xml = xml.encode(self.encoding, 'ignore')

        root = etree.fromstring(xml, parser=self.parser)

        for analytics_gn in root.iterfind('.//media:thumbnail', namespaces=self.namespaces):
            analytics_gn.tag = "{%s}image" % analytics_gn.nsmap['media']

        return etree.tostring(root, encoding='utf-8').decode('utf-8', 'ignore')


replaced_xml = ElementsTagReplacer().replace(xml)

Recommended Posts

Rewrite the name of the namespaced tag with lxml
Specify the project name of docker-compose with Docker integration of Pycharm
Rewrite the record addition node of SPSS Modeler with Python.
Get the host name of the host PC with Docker on Linux
Rewrite the sampling node of SPSS Modeler with Python (2): Layered sampling, cluster sampling
Align the size of the colorbar with matplotlib
Check the existence of the file with python
[Python] Get the variable name with str
[Python3] Rewrite the code object of the function
The third night of the loop with for
When changing the table name with flask_migrate
The second night of the loop with for
Set the process name of the Python program
Count the number of characters with echo
Rewrite the sampling node of SPSS Modeler with Python ①: First N cases, random sampling
The story of doing deep learning with TPU
Note: Prepare the environment of CmdStanPy with docker
Prepare the execution environment of Python3 with Docker
(Note) Importing Excel with the same column name
[Note] Export the html of the site with python.
See the behavior of drunkenness with reinforcement learning
Increase the font size of the graph with matplotlib
Calculate the total number of combinations with python
Check the date of the flag duty with Python
Get git branch name and tag name with python
Eliminate the inconveniences of QDock Widget with PySide
Challenge the Tower of Hanoi with recursion + stack
Fill the browser with the width of Jupyter Notebook
Predict the second round of summer 2016 with scikit-learn
Dump the contents of redis db with lua
Tucker decomposition of the hay process with HOOI
Find out the day of the week with datetime
The basis of graph theory with matplotlib animation
Visualize the behavior of the sorting algorithm with matplotlib
Convert the character code of the file with Python3
[Python] Determine the type of iris with SVM
Contents of __name__
Extract the table of image files with OneDrive & Python
Get the variable name of the variable as a character string.
Add the attribute of the object of the class with the for statement
Coordinates of the right end of Label made with tkinter
The story of stopping the production service with the hostname command
Learn Nim with Python (from the beginning of the year).
Find the sum of unique values with pandas crosstab
Play with the UI implementation of Pythonista3 [Super Super Introduction]
The story of replacing Nvidia GTX 1650 with Linux Mint 20.1.
Add information to the bottom of the figure with Matplotlib
Get the sum of each of multiple columns with awk
The story of sharing the pyenv environment with multiple users
Destroy the intermediate expression of the sweep method with Python
Take a screenshot of the LCD with Python-LEGO Mindstorms
Identify the name from the flower image with keras (tensorflow)
Visualize the range of interpolation and extrapolation with python
Calculate the regression coefficient of simple regression analysis with python
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Predict the number of people infected with COVID-19 with Prophet
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Find out the location of packages installed with pip
Predict the gender of Twitter users with machine learning
Try to get the contents of Word with Golang
Visualize the characteristic vocabulary of a document with D3.js