Not receiving output from json-parser in python -


this incredibly basic: had used python parsing json files in single directory several months ago. can't figure out how tweaked (a teammate came code) can data more useable csv format.

at moment, i'm getting zilch when using python launcher or terminal.

what parser looks like:

import codecs import json import os import sys  try:     import unicodecsv csv     except importerror:     import csv      output_file = 'output.csv'   def process_file(infile, writer):     print('processing file: %s' % infile)     codecs.open(infile, encoding='utf-8') infile:         data = json.load(infile)             item in data:                 _id = item['id']                 description =  item['description']                 gov in item['source']:                     gov_id = gov['name']                     source in item['secondarysource']:                         source_id = source['sourceid']                             name = source['name']                             party = source['party']                             writer.writerow([_id, description, gov_id, source_id, name, party])   def process_files_in_directory(directory, outfile):      codecs.open(outfile, 'w') outfile:          writer = csv.writer(outfile)          writer.writerow(["id", "description", "branch", "sourceid", "name", "party"])          f in os.listdir(path):              if f.endswith('.json'):                  process_file(f, writer)  usage = """ usage:              python json_parser.py <source_directory> [<output_file>]              source_directory path directory input json files.             output_file optional -- defaults %s             file names must end .json             """ % output_file   if __name__=='__main__':     try:         directory = sys.argv[1]     except indexerror:         print(usage)         sys.exit(0)     if len(sys.argv) > 2:         outfile = sys.argv[2]     else:         outfile = output_file     process_files_in_directory(directory, outfile) 

your script has formatting issues. i'm not sure if related issues... here's new version of script. basic idea works, might want format csv output make more readable. prove works i've run command-line with:

python stackoverflow\junk.py stackoverflow\mydir 

where stackoverflow\mydir has 2 files: one.json , two.json.

the code below includes fix comment above

import codecs import json import os import sys  try:     import unicodecsv csv except importerror:     import csv  output_file = 'output.csv'   def process_file(infile, writer):     print('processing file: %s' % infile)     codecs.open(infile, encoding='utf-8') infile:         data = json.load(infile)         item in data:             _id = item['id']             description = item['description']             gov in item['source']:                 gov_id = gov['name']                 source in item['secondarysource']:                     source_id = source['sourceid']                     name = source['name']                     party = source['party']                     writer.writerow([_id, description, gov_id, source_id, name, party])   def process_files_in_directory(directory, outfile):     codecs.open(outfile, 'w') outfile:         writer = csv.writer(outfile)         writer.writerow(["id", "description", "branch", "sourceid", "name", "party"])         f in os.listdir(directory):             if f.endswith('.json'):                 process_file(os.path.join(directory, f), writer)  usage = """ usage:              python json_parser.py <source_directory> [<output_file>]              source_directory path directory input json files.             output_file optional -- defaults %s             file names must end .json             """ % output_file  if __name__ == '__main__':     try:         directory = sys.argv[1]     except indexerror:         print(usage)         sys.exit(0)     if len(sys.argv) > 2:         outfile = sys.argv[2]     else:         outfile = output_file     process_files_in_directory(directory, outfile) 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -