[英]find multiple elements in one loop for python
I have to insert a few XML tags into the SQL database.我必须在 SQL 数据库中插入一些 XML 标签。
I am able to get values by the below code.我可以通过以下代码获取值。 But I have to insert multiple values like created, comments, name, modify, etc. How can I get all in one call and insert it in one statement into SQL?但我必须插入多个值,如创建、评论、名称、修改等。我怎样才能在一次调用中获取所有内容并将其插入到 SQL 的一条语句中?
for element in root.iter('Created'):
print(element.text)
You have several options:你有几个选择:
With itertools.chain() :使用itertools.chain() :
tags = ['Created', 'Comments', 'Name', 'Modify']
tag_iterators = [root.iter(tag) for tag in tags]
for element in itertools.chain(*tag_iterators):
print(element.text)
With iterating through all tags and then filtering:遍历所有标签然后过滤:
tags = ['Created', 'Comments', 'Name', 'Modify']
for element in root.iter():
if element.tag not in tags:
continue
print(element.text)
Finally, if you need all the tags, then simply:最后,如果您需要所有标签,那么只需:
for element in root.iter():
print(element.text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.