Hello,
I have the following questions about using BS:
What's the difference between...
1. .decompose() and .extract()? And how to remove empty lines after running either?
2. soup.select_one() and soup.kml.Document.name?
3. .string and .text?
Thank you.
Edit: Here's how to remove empty lines after using decompose()/extract():
I have the following questions about using BS:
What's the difference between...
1. .decompose() and .extract()? And how to remove empty lines after running either?
2. soup.select_one() and soup.kml.Document.name?
3. .string and .text?
Thank you.
INPUTFILE="blah.kml"
PATH=pathlib.Path(item).parent
BASENAME = pathlib.Path(item).stem
EXTENSION = pathlib.Path(item).suffix
OUTPUTFILE = f"{BASENAME}.EDITED{EXTENSION}"
soup = BeautifulSoup(open(INPUTFILE, 'r',encoding='utf-8'), features='xml')
for snippet in soup.find_all(['description', 'ExtendedData']):
#snippet.decompose()
snippet.extract()
soup.smooth()
name = soup.select_one("kml > Document > name")
#WHY BAD? name = soup.kml.Document.name
if name:
print("Name found")
name.string = BASENAME
#AttributeError: property 'text' of 'Tag' object has no setter
#name.text = BASENAME
else:
print("No name")
name = soup.new_tag("name")
#name.text = BASENAME
name.string = BASENAME
doc = soup.kml.Document
doc.insert(0,name)
with open(OUTPUTFILE, "w",encoding='utf-8') as file:
file.write(str(soup)) --Edit: Here's how to remove empty lines after using decompose()/extract():
with open(OUTPUTFILE, "w",encoding='utf-8') as file: #file.write(str(soup)) file.write(str(BeautifulSoup(str(soup), features='xml')))
