以下是我要格式化的内容
{
"url":"https://www.w3schools.com/",
"originalUrl":"https://www.w3schools.com/",
"applications":[
{
"name":"EdgeCast",
"confidence":"100",
"version":"",
"icon":"EdgeCast.png",
"categories":[
"CDN"
]
},
,
{
"name":"Google Analytics",
"confidence":"100",
"version":"UA",
"icon":"Google Analytics.svg",
"categories":[
"Analytics"
]
},
{
"name":"Microsoft ASP.NET",
"confidence":"50",
"version":"",
"icon":"Microsoft ASP.NET.png",
"categories":[
"Web Frameworks"
]
},
{
"name":"IIS",
"confidence":"25",
"version":"",
"icon":"IIS.png",
"categories":[
"Web Servers"
]
},
{
"name":"Windows Server",
"confidence":"25",
"version":"",
"icon":"Microsoft.svg",
"categories":[
"Operating Systems"
]
}
]
}
当我使用Python脚本使用下面提到的代码片段编译它时,它显然是在终端内显示内容
for criteria in d['applications']:
for key, value in criteria.iteritems():
print key, 'is:', value
print ''
它将以下输出提供给终端:
confidence is: 100
version is: ``
name is: EdgeCast
categories is: [u'CDN']
icon is: EdgeCast.png
confidence is: 100
version is: UA
name is: Google Analytics
categories is: [u'Analytics']
icon is: Google Analytics.svg
confidence is: 50
version is:
name is: Microsoft ASP.NET
categories is: [u'Web Frameworks']
icon is: Microsoft ASP.NET.png
我需要写一个文本文件。我应该能够同时添加写多个参数来写入文本文件
答案 0 :(得分:0)
你需要在python中进行文件I / o。使用以下命令打开文件:
with open("your-file-name", 'w') as out:
for criteria in d['applications']:
for key, value in criteria.iteritems():
out.write "{} is: {}\n".format(key, value)
这里有几个概念:使用写入模式打开文件,并使用字符串格式打印变量。