How to add an xml tag based on specific condition using python

Yugam Uppal

sample XML File

<ArticleSet>
    <Article>
        <ForeName>a</ForeName>
        <LastName>b</LastName>
        <Affiliation>harvard university of science. [email protected]</Affiliation>
        <Keywords>-</Keywords>
    </Article>
    <Article>
        <ForeName>a</ForeName>
        <LastName>b</LastName>
        <Affiliation>-</Affiliation>
        <Keywords>-</Keywords>
    </Article>
    <Article>
        <ForeName>a</ForeName>
        <LastName>b</LastName>
        <Affiliation>harvard university of science. [email protected]</Affiliation>
        <Keywords>-</Keywords>
    </Article>
</ArticleSet>

SAMPLE CODE

from xml.etree import ElementTree as etree
import re

root = etree.parse("sampleinput.xml").getroot()

for article in root.iter("Affiliation"):
    if(article.text != "-"):
        email = re.search(r'[\w\.-]+@[\w\.-]+', article.text)
        c = etree.Element("<Email>")
        c.text = email.group(0)
        etree.write(article,c)

OUTPUT REQUIRED UPDATED XML FILE

<?xml version="1.0"?>
<ArticleSet>
    <Article>
        <ForeName>a</ForeName>
        <LastName>b</LastName>
        <Affiliation>harvard university of science. [email protected]</Affiliation>
        <Keywords>-</Keywords>
        <Email>[email protected]</Email>
    </Article>
    <Article>
        <ForeName>a</ForeName>
        <LastName>b</LastName>
        <Affiliation>-</Affiliation>
        <Keywords>-</Keywords>
        <Email>-</Email>
    </Article>
    <Article>
        <ForeName>a</ForeName>
        <LastName>b</LastName>
        <Affiliation>harvard university of science. [email protected]</Affiliation>
        <Keywords>-</Keywords>
        <Email>[email protected]</Email>
    </Article>
</ArticleSet>

I want to extract email address from <Affiliation> tag and make a new tag named <Email> and store extracted email into that tag. If <Affiliation> is equal to - then store <Email>-</Email> into that article.

ERROR

Traceback (most recent call last): File "C:/Users/Ghost Rider/Documents/Python/addingTagsToXML.py", line 11, in etree.write(article,c) AttributeError: module 'xml.etree.ElementTree' has no attribute 'write'

Vikas P

You can try this :

import re
import xml
tree = xml.etree.ElementTree.parse('filename.xml')
e = tree.getroot()

for article in e.findall('Article'):
    child = xml.etree.ElementTree.Element("Email")
    if article[2].text != '-':
        email = re.search(r'[\w\.-]+@[\w\.-]+', article[2].text).group()
        child.text = email
    else:
        child.text = ' - '
    article.insert(4,child)
tree.write("filename.xml")

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to add xml-stylesheet tag to XML file using python?

分類Dev

How to add class to a tag on different condition using jquery?

分類Dev

How to replace "\" and "\\" for only a specific tag in XML?

分類Dev

How do i sum net amount based on policyno in xml tag by using Xquery

分類Dev

How to Extract Specific XML lines from an API response using Python

分類Dev

Get the length based on specific condition using Angular.js

分類Dev

Updating XML node value using the condition in Python

分類Dev

Pygsheets: How to highlight cells in a specific column based on a condition

分類Dev

how to add column in dataframe based on some condition from another column?

分類Dev

How to add a child via PHP in xml string with soap tag

分類Dev

How to add an attribute in a tag in XML file by command-line?

分類Dev

Using XQuery to select a specific value from a specific (conjoined) tag in an xml database

分類Dev

How to Find a tag without specific attribute using beautifulsoup?

分類Dev

How to add a field with specific datatype ( list, object) in an existing mongoDB document using python

分類Dev

how to add a specific value to a column iteratively in python

分類Dev

How to output an IMG tag from XML using XSLT

分類Dev

How to add a condition inside or after .Select() using Linq EF

分類Dev

using awk to parse a specific condition

分類Dev

How to find a string inside a XML like tag from a file in Python?

分類Dev

Add android:name to application tag of AndroidManifest.xml using Cordova Plugin

分類Dev

xmlstarlet, find a title in XML based on the child tag

分類Dev

xmlstarlet, find a title in XML based on the child tag

分類Dev

Python: In Pandas extract data from several columns in a dataframe based on a condition and add to different dataframe matching on a column

分類Dev

Python Marshmallow condition based validation

分類Dev

How to narrow an array for a specific condition?

分類Dev

How to read specific tags using XML2

分類Dev

How do I add up values from different columns based on a condition of the column right next to it?

分類Dev

Add tag around matches in Python

分類Dev

How to change views based on condition

Related 関連記事

  1. 1

    How to add xml-stylesheet tag to XML file using python?

  2. 2

    How to add class to a tag on different condition using jquery?

  3. 3

    How to replace "\" and "\\" for only a specific tag in XML?

  4. 4

    How do i sum net amount based on policyno in xml tag by using Xquery

  5. 5

    How to Extract Specific XML lines from an API response using Python

  6. 6

    Get the length based on specific condition using Angular.js

  7. 7

    Updating XML node value using the condition in Python

  8. 8

    Pygsheets: How to highlight cells in a specific column based on a condition

  9. 9

    how to add column in dataframe based on some condition from another column?

  10. 10

    How to add a child via PHP in xml string with soap tag

  11. 11

    How to add an attribute in a tag in XML file by command-line?

  12. 12

    Using XQuery to select a specific value from a specific (conjoined) tag in an xml database

  13. 13

    How to Find a tag without specific attribute using beautifulsoup?

  14. 14

    How to add a field with specific datatype ( list, object) in an existing mongoDB document using python

  15. 15

    how to add a specific value to a column iteratively in python

  16. 16

    How to output an IMG tag from XML using XSLT

  17. 17

    How to add a condition inside or after .Select() using Linq EF

  18. 18

    using awk to parse a specific condition

  19. 19

    How to find a string inside a XML like tag from a file in Python?

  20. 20

    Add android:name to application tag of AndroidManifest.xml using Cordova Plugin

  21. 21

    xmlstarlet, find a title in XML based on the child tag

  22. 22

    xmlstarlet, find a title in XML based on the child tag

  23. 23

    Python: In Pandas extract data from several columns in a dataframe based on a condition and add to different dataframe matching on a column

  24. 24

    Python Marshmallow condition based validation

  25. 25

    How to narrow an array for a specific condition?

  26. 26

    How to read specific tags using XML2

  27. 27

    How do I add up values from different columns based on a condition of the column right next to it?

  28. 28

    Add tag around matches in Python

  29. 29

    How to change views based on condition

ホットタグ

アーカイブ