2 回答
TA贡献1111条经验 获得超0个赞
您可以迭代字典列表并使用在每条记录中找到的主机列表扩展“主机”列表。当您拥有所有值时,构建逗号分隔的字符串。
import json
# python 2.7 needs different open
import codecs
filename = "output.json"
# json is usually utf-8 encoded but this is not 100% guaranteed
data = json.loads(codecs.open(filename, encoding="utf-8"))
hosts = []
for record in data:
if "specific_data.data.hostname" in record:
hosts.extend(record["specific_data.data.hostname"])
# assuming hosts are all ascii
with open("hostnames.csv", "w") as fileobj:
fileobj.write(",".join(hosts))
fileobj.write("\n")
TA贡献1796条经验 获得超10个赞
以下将csv变量设置为以逗号分隔的主机名的字符串。
import json
object = json.loads(snippet)
csv = ', '.join(x['specific_data.data.hostname'][0] for x in object)
添加回答
举报