0

Here is my content that I am going to to format

{
 "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"
     ]
  }
]
}

When I compile this using Python script using below mentioned code snippet it is clearly displaying the content inside the terminal

for criteria in d['applications']:
    for key, value in criteria.iteritems():
        print key, 'is:', value
        print ''

It gives the following output into the terminal:

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

I need to write as it is to a text file.Here I should be able to add write multiple arguments at the same time to write to a text file

Prageeth
  • 209
  • 1
  • 2
  • 4

1 Answers1

0

You need to do file I/o in python. Open the file using:

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)

There's a couple of concepts here: open the file with write mode, and also using a string format to print your variable.

Owen Hempel
  • 434
  • 2
  • 8