Skip to main content
added 173 characters in body
Source Link
Justin O Barber
  • 11.6k
  • 2
  • 43
  • 45

Updated

I see what you want now, I think. Below is how I would do it. Note that you will need to apply this to the parent element that contains a movie, TV show, etc. Also note that case matters (see note in code below).

First, the function:

>>> xml = '''
<Root>
 <Movie>
  <Provider>xxx2</Provider>
  <Title>The World's Fastest Indian</Title>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </Movie>
 <TVSeries>
  <Provider>xxx</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description> The World's Fastest Indian </Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
  </TVSeries> // note that I changed the v to an upper-case V
</Root>'''
>>> root = ET.fromstring(xml)
>>> def insert_description(element):
    '''Inserts the Title as a Description if Desscription not present.'''
    for sub_e in element:
        if sub_e.find('Description') is None:
            title = sub_e.find('Title').text
            new_desc = ET.Element('Description')
            new_desc.text = title
            sub_e.insert(2, new_desc)

Now to test the function:

>>> xml = '''
<Root>
 <Movie>
  <Provider>xxx2</Provider>
  <Title>The World's Fastest Indian</Title>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </Movie>
 <TVSeries>
  <Provider>xxx</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description> The World's Fastest Indian </Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
  </TVSeries> // note that I changed the v to an upper-case V
</Root>'''
>>> root = ET.fromstring(xml)
>>> insert_description(root)
>>> print ET.tostring(root)
<Root>
 <Movie>
  <Provider>xxx2</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description>The World's Fastest Indian</Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </Movie>
 <TVSeries>
  <Provider>xxx</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description> The World's Fastest Indian </Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </TVSeries> // note that I changed the v to an upper-case V
</Root>

I formatted the latter output with indentation to make what has happened clearer.

Updated

I see what you want now, I think. Below is how I would do it. Note that you will need to apply this to the parent element that contains a movie, TV show, etc. Also note that case matters (see note in code below):

>>> xml = '''
<Root>
 <Movie>
  <Provider>xxx2</Provider>
  <Title>The World's Fastest Indian</Title>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </Movie>
 <TVSeries>
  <Provider>xxx</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description> The World's Fastest Indian </Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
  </TVSeries> // note that I changed the v to an upper-case V
</Root>'''
>>> root = ET.fromstring(xml)
>>> def insert_description(element):
    '''Inserts the Title as a Description if Desscription not present.'''
    for sub_e in element:
        if sub_e.find('Description') is None:
            title = sub_e.find('Title').text
            new_desc = ET.Element('Description')
            new_desc.text = title
            sub_e.insert(2, new_desc)

            
>>> insert_description(root)
>>> print ET.tostring(root)
<Root>
 <Movie>
  <Provider>xxx2</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description>The World's Fastest Indian</Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </Movie>
 <TVSeries>
  <Provider>xxx</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description> The World's Fastest Indian </Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </TVSeries> // note that I changed the v to an upper-case V
</Root>

I formatted the latter output with indentation to make what has happened clearer.

Updated

I see what you want now, I think. Below is how I would do it. Note that you will need to apply this to the parent element that contains a movie, TV show, etc. Also note that case matters (see note in code below).

First, the function:

def insert_description(element):
    '''Inserts the Title as a Description if Desscription not present.'''
    for sub_e in element:
        if sub_e.find('Description') is None:
            title = sub_e.find('Title').text
            new_desc = ET.Element('Description')
            new_desc.text = title
            sub_e.insert(2, new_desc)

Now to test the function:

>>> xml = '''
<Root>
 <Movie>
  <Provider>xxx2</Provider>
  <Title>The World's Fastest Indian</Title>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </Movie>
 <TVSeries>
  <Provider>xxx</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description> The World's Fastest Indian </Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
  </TVSeries> // note that I changed the v to an upper-case V
</Root>'''
>>> root = ET.fromstring(xml)
>>> insert_description(root)
>>> print ET.tostring(root)
<Root>
 <Movie>
  <Provider>xxx2</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description>The World's Fastest Indian</Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </Movie>
 <TVSeries>
  <Provider>xxx</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description> The World's Fastest Indian </Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </TVSeries> // note that I changed the v to an upper-case V
</Root>

I formatted the latter output with indentation to make what has happened clearer.

added 173 characters in body
Source Link
Justin O Barber
  • 11.6k
  • 2
  • 43
  • 45

You need to use xml.etree.ElementTree.SubElement(parent, tag, attrib={}, **extra) to insert an element instance rather than just a description (string). You have a lot of code thereUpdated

I see what you want now, butI think. Below is how I would do it appears. Note that you are tryingwill need to insert a description (rather than an element instance) here:

child.insert(2,"Description")

Tryapply this instead:

m = tree.find('./Movie')  # The parent we want to create an element under
child.insert(2, ET.SubElement(m, "Description"))

Here isto the parent element that contains a working examplemovie, TV show, etc. Also note that case matters (I had some trouble with your treesee note in code below):

>>> treexml = ET.fromstring('''<Root>'''
<Root>
 <Movie>
  <Provider>xxx2</Provider>
  <Title>The World's Fastest Indian</Title>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </Movie>
 <TVSeries>
  <Provider>xxx</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description> descThe World's Fastest Indian </Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
  </TVSeries><TVSeries> // note that I changed the v to an upper-case V
</Root>''')
>>> mroot = treeET.findfromstring('./Movie'xml)
>>> treedef insert_description(element):
    '''Inserts the Title as a Description if Desscription not present.insert'''
    for sub_e in element:
        if sub_e.find(2,'Description') is None:
            title = sub_e.find('Title').text
            new_desc = ET.SubElementElement(m'Description')
            new_desc.text = title
            sub_e.insert(2, "Description"new_desc)

            
>>> insert_description(root)
>>> print ET.tostring(treeroot)
"<Root>\n<Movie>\n<Provider>xxx2<<Root>
 <Movie>
  <Provider>xxx2</Provider>\n<Title>TheProvider>
  <Title>The World's Fastest Indian</Title>\n<SortTitle>World'sTitle>
  <Description>The World's Fastest Indian</Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>\n<DescriptionSortTitle>
 /><</Movie>\n<TVSeries>\n<Provider>xxx<Movie>
 <TVSeries>
  <Provider>xxx</Provider>\n<Title>TheProvider>
  <Title>The World's Fastest Indian</Title>\n<Description>Title>
 desc <Description> The World's Fastest Indian </Description>\n<SortTitle>World'sDescription>
  <SortTitle>World's Fastest Indian, The</SortTitle>\n<SortTitle>
 </TVSeries><DescriptionTVSeries> /></Root>" note that I changed the v to an upper-case V
</Root>

ET.SubElement("Movie", "Description") creates a new sub-element instance I formatted the latter output with indentation to make what has happened clearer.

You need to use xml.etree.ElementTree.SubElement(parent, tag, attrib={}, **extra) to insert an element instance rather than just a description (string). You have a lot of code there, but it appears that you are trying to insert a description (rather than an element instance) here:

child.insert(2,"Description")

Try this instead:

m = tree.find('./Movie')  # The parent we want to create an element under
child.insert(2, ET.SubElement(m, "Description"))

Here is a working example (I had some trouble with your tree):

>>> tree = ET.fromstring('''<Root>
<Movie>
<Provider>xxx2</Provider>
<Title>The World's Fastest Indian</Title>
<SortTitle>World's Fastest Indian, The</SortTitle>
</Movie>
<TVSeries>
<Provider>xxx</Provider>
<Title>The World's Fastest Indian</Title>
<Description> desc </Description>
<SortTitle>World's Fastest Indian, The</SortTitle>
</TVSeries></Root>''')
>>> m = tree.find('./Movie')
>>> tree.insert(2, ET.SubElement(m, "Description"))
>>> ET.tostring(tree)
"<Root>\n<Movie>\n<Provider>xxx2</Provider>\n<Title>The World's Fastest Indian</Title>\n<SortTitle>World's Fastest Indian, The</SortTitle>\n<Description /></Movie>\n<TVSeries>\n<Provider>xxx</Provider>\n<Title>The World's Fastest Indian</Title>\n<Description> desc </Description>\n<SortTitle>World's Fastest Indian, The</SortTitle>\n</TVSeries><Description /></Root>"

ET.SubElement("Movie", "Description") creates a new sub-element instance.

Updated

I see what you want now, I think. Below is how I would do it. Note that you will need to apply this to the parent element that contains a movie, TV show, etc. Also note that case matters (see note in code below):

>>> xml = '''
<Root>
 <Movie>
  <Provider>xxx2</Provider>
  <Title>The World's Fastest Indian</Title>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </Movie>
 <TVSeries>
  <Provider>xxx</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description> The World's Fastest Indian </Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
  </TVSeries> // note that I changed the v to an upper-case V
</Root>'''
>>> root = ET.fromstring(xml)
>>> def insert_description(element):
    '''Inserts the Title as a Description if Desscription not present.'''
    for sub_e in element:
        if sub_e.find('Description') is None:
            title = sub_e.find('Title').text
            new_desc = ET.Element('Description')
            new_desc.text = title
            sub_e.insert(2, new_desc)

            
>>> insert_description(root)
>>> print ET.tostring(root)
<Root>
 <Movie>
  <Provider>xxx2</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description>The World's Fastest Indian</Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </Movie>
 <TVSeries>
  <Provider>xxx</Provider>
  <Title>The World's Fastest Indian</Title>
  <Description> The World's Fastest Indian </Description>
  <SortTitle>World's Fastest Indian, The</SortTitle>
 </TVSeries> // note that I changed the v to an upper-case V
</Root>

I formatted the latter output with indentation to make what has happened clearer.

added 554 characters in body
Source Link
Justin O Barber
  • 11.6k
  • 2
  • 43
  • 45

You need to use xml.etree.ElementTree.SubElement(parent, tag, attrib={}, **extra) to insert an element instance rather than just a description (string). You have a lot of code there, but it appears that you are trying to insert a description (rather than an element instance) here:

child.insert(2,"Description")

Try this instead:

m = tree.find('./Movie')  # The parent we want to create an element under
child.insert(2, ET.SubElement(m, "Description"))

Here is a working example (I had some trouble with your tree):

>>> tree = ET.fromstring('''<Root>
<Movie>
<Provider>xxx2</Provider>
<Title>The World's Fastest Indian</Title>
<SortTitle>World's Fastest Indian, The</SortTitle>
</Movie>
<TVSeries>
<Provider>xxx</Provider>
<Title>The World's Fastest Indian</Title>
<Description> desc </Description>
<SortTitle>World's Fastest Indian, The</SortTitle>
</TVSeries></Root>''')
>>> m = tree.find('./Movie')
>>> tree.insert(2, ET.SubElement(m, "Description"))
>>> ET.tostring(tree)
"<Root>\n<Movie>\n<Provider>xxx2</Provider>\n<Title>The World's Fastest Indian</Title>\n<SortTitle>World's Fastest Indian, The</SortTitle>\n<Description /></Movie>\n<TVSeries>\n<Provider>xxx</Provider>\n<Title>The World's Fastest Indian</Title>\n<Description> desc </Description>\n<SortTitle>World's Fastest Indian, The</SortTitle>\n</TVSeries><Description /></Root>"

ET.SubElement("Movie", "Description") creates a new sub-element instance.

You need to use xml.etree.ElementTree.SubElement(parent, tag, attrib={}, **extra) to insert an element instance rather than just a description (string). You have a lot of code there, but it appears that you are trying to insert a description (rather than an element instance) here:

child.insert(2,"Description")

Try this instead:

m = tree.find('./Movie')
child.insert(2, ET.SubElement(m, "Description"))

Here is a working example (I had some trouble with your tree):

>>> tree = ET.fromstring('''<Root>
<Movie>
<Provider>xxx2</Provider>
<Title>The World's Fastest Indian</Title>
<SortTitle>World's Fastest Indian, The</SortTitle>
</Movie>
<TVSeries>
<Provider>xxx</Provider>
<Title>The World's Fastest Indian</Title>
<Description> desc </Description>
<SortTitle>World's Fastest Indian, The</SortTitle>
</TVSeries></Root>''')
>>> m = tree.find('./Movie')
>>> tree.insert(2, ET.SubElement(m, "Description"))
>>> ET.tostring(tree)
"<Root>\n<Movie>\n<Provider>xxx2</Provider>\n<Title>The World's Fastest Indian</Title>\n<SortTitle>World's Fastest Indian, The</SortTitle>\n<Description /></Movie>\n<TVSeries>\n<Provider>xxx</Provider>\n<Title>The World's Fastest Indian</Title>\n<Description> desc </Description>\n<SortTitle>World's Fastest Indian, The</SortTitle>\n</TVSeries><Description /></Root>"

ET.SubElement("Movie", "Description") creates a new sub-element instance.

You need to use xml.etree.ElementTree.SubElement(parent, tag, attrib={}, **extra) to insert an element instance rather than just a description (string). You have a lot of code there, but it appears that you are trying to insert a description (rather than an element instance) here:

child.insert(2,"Description")

Try this instead:

m = tree.find('./Movie')  # The parent we want to create an element under
child.insert(2, ET.SubElement(m, "Description"))

Here is a working example (I had some trouble with your tree):

>>> tree = ET.fromstring('''<Root>
<Movie>
<Provider>xxx2</Provider>
<Title>The World's Fastest Indian</Title>
<SortTitle>World's Fastest Indian, The</SortTitle>
</Movie>
<TVSeries>
<Provider>xxx</Provider>
<Title>The World's Fastest Indian</Title>
<Description> desc </Description>
<SortTitle>World's Fastest Indian, The</SortTitle>
</TVSeries></Root>''')
>>> m = tree.find('./Movie')
>>> tree.insert(2, ET.SubElement(m, "Description"))
>>> ET.tostring(tree)
"<Root>\n<Movie>\n<Provider>xxx2</Provider>\n<Title>The World's Fastest Indian</Title>\n<SortTitle>World's Fastest Indian, The</SortTitle>\n<Description /></Movie>\n<TVSeries>\n<Provider>xxx</Provider>\n<Title>The World's Fastest Indian</Title>\n<Description> desc </Description>\n<SortTitle>World's Fastest Indian, The</SortTitle>\n</TVSeries><Description /></Root>"

ET.SubElement("Movie", "Description") creates a new sub-element instance.

added 554 characters in body
Source Link
Justin O Barber
  • 11.6k
  • 2
  • 43
  • 45
Loading
Source Link
Justin O Barber
  • 11.6k
  • 2
  • 43
  • 45
Loading