Python在信息处理与文档生成中的应用
1. Python文本处理与ElementTree库
在Python中,文本处理是一项重要的任务。例如,使用ElementTree库可以方便地解析XML数据。以下是一段示例代码,用于查找XML中特定标签下的数据:
if __name__ == '__main__':
for d in e.findall('/array/dict'):
if d.find('string').text == 'SPSoftwareDataType':
sp_data = d.find('array').find('dict')
break
else:
print "SPSoftwareDataType NOT FOUND"
sys.exit(1)
record = []
for child in sp_data.getchildren():
record.append(child.text)
if child.tag == 'string':
print "%-15s -> %s" % tuple(record)
record = []
这段代码的逻辑如下:
1. 遍历XML中所有的 dict 标签。
2. 找到包含 string 子元素且其文本值为 SPSoftwareDa
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



