Python Forum

Full Version: csv output code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi how can i create an output file for csv file on this code

import whoozy
import json
import sys

whoozy.key = '458dddfigglfklskjkjfljl'

if len(sys.argv) < 2:
  sys.exit('Usage: cli.py domain [role]')

domain = sys.argv[1]
role   = sys.argv[2]
people = clearbit.Prospector.search(domain=domain, role=role, email=True)

for person in people:
  print(json.dumps(person, sort_keys=True, indent=4))
change this:
for person in people:
 print(json.dumps(person, sort_keys=True, indent=4))
to
with open('put filename here', 'w') as fout:
   for person in people:
       json.dumps(person, fout)
untested - you will also have to add the commas for a csv file

it's hard to do without knowing what 'person' looks like. If you provide an example,
the code can be provided.
heres the output when i run the script


C:\Users\IT02\bdh>test.py worleyparsons.com sales
{redacted}

C:\Users\IT02\bdh>