我已将 xml 中的字符串存储为
< xml >
< path > /home/user/Desktop/Folder < / path >
现在,使用python我想从字符串中提取所有路径并将它们存储在列表中。
实施例: 列表应包含所有这些路径:
home
home/user
home/user/Desktop
home/user/Desktop/Folder
答案 0 :(得分:1)
从xml中获取字符串 - (我使用过ElementTree)
#importing ElementTree
>>> import xml.etree.ElementTree as ET
>>> xml_string = '''<xml><path>/home/user/Desktop/Folder</path></xml>'''
>>> root = ET.fromstring(xml_string)
>>> s = root.find("path").text
>>> print s
/home/user/Desktop/Folder
获取字符串后,您可以解析它 -
#Logic for splitting the contents
>>> split_li = s.split("/")
>>> ["/".join(split_li[:i]) for i in range(2, len(split_li) + 1)]
['/home', '/home/user', '/home/user/Desktop', '/home/user/Desktop/Folder']