tjack5

studentinfo_code

Dec 11th, 2022
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | Source Code | 0 0
  1. import pandas as pd
  2. import re
  3. from lxml import etree as Etr
  4. #import ElementTree
  5. from xml.etree import ElementTree as ET
  6. from xml.parsers import expat
  7. from bs4 import BeautifulSoup
  8. import pandas_read_xml as pdx
  9.  
  10. with open('studentinfo.xml', 'r') as f:
  11.     file = f.read()
  12. soup = BeautifulSoup(file, 'lxml') ## do NOT use 'xml' parser here unless you want to lose namespaces
  13.  
  14. enumTags = ['st:name']
  15. for d in [c for c in soup.descendants if c.name]:
  16.     for name in enumTags:
  17.         for i, t in enumerate(d.find_all(name, recursive=False)):
  18.             t.name = f'{t.name}{i}'
  19.             print(soup.prettify)
  20.    
Tags: python bs4
Advertisement
Add Comment
Please, Sign In to add comment