Created
February 1, 2019 08:42
-
-
Save VioletVivirand/81e9dfbfc27ebd56cf54de186c1f5c6c to your computer and use it in GitHub Desktop.
BeautifulSoup with XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
# Read XML with XML parser | |
with open('template.xml') as fp: | |
soup = BeautifulSoup(fp, 'xml') | |
# Modify value of a tag | |
soup.size.width.string = 'Something New' | |
# Equal to soup.annotation.size.width | |
# Add a tag | |
obj_tag = soup.new_tag('object') | |
soup.annotation.append(obj_tag) | |
# Print with prettified text | |
print(soup.prettify()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment