mpd = ''
file1 = open("/manifest.mpd", "r", encoding = "utf-8")
while True:
line = file1.readline() # считываем строку
if not line: break # прерываем цикл, если строка пустая
if line.strip() == '</Period>':
mpd += f' <AdaptationSet id="1" mimeType="image/webp" contentType="image">\n'
mpd += ' </AdaptationSet>\n'
mpd += line
file1.write(mpd)
#print(mpd)
file1.close()
一切都是错误的,您需要读取 mpd 文件,找到该条目,在前面添加几行并将其保存在同一个文件中。我们找到它,添加它,但无法保存它。我尝试了所有模式,最后要么是错误,要么是空文件
最好的方法是什么?
**************我正在尝试弄清楚如何与xml.etree.ElementTree
tree = ET.parse('/manifest.mpd')
root = tree.getroot()
new_elem = ET.Element('new_tag')
new_elem.set('attribute_name', 'attribute_value')
new_elem.text = 'element_tesssssssxt'
root.findall('AdaptationSet').append(new_elem)
我不明白,他读,改变……
假设您需要添加一行,在最后一个元素之后添加AdaptationSet
这该怎么办,我可以添加线路吗?保存后,所有标签都以ns0:
root.find("{urn:mpeg:dash:schema:mpd:2011}AdaptationSet")
不起作用
安装lxml,代码是这样的。