Re: Can anybody help me retrieve how to retrieve output from this Python code below!

2017-07-11 Thread Peter Otten
ksatish@gmail.com wrote:

[snip code]

Wasn't there any documentation to go with that script? That's the preferable 
method to use software written by someone else ;)


Anyway -- First you have to undo what was probably changed by yourself:

$ diff -u json2csv_orig.py json2csv.py
--- json2csv_orig.py2017-07-11 15:15:06.527571509 +0200
+++ json2csv.py 2017-07-11 15:14:17.878514787 +0200
@@ -132,14 +132,6 @@
 
 return parser
 
-json_file = input("Type Json input file name: ")
-
-key_map = input("Type Key value : ")
-
-MultiLineJson2Csv(Json2Csv).init_parser()
-
-Json2Csv.load(json_file)
-
 
 if __name__ == '__main__':
 parser = init_parser()
@@ -159,4 +151,4 @@
 fileName, fileExtension = os.path.splitext(args.json_file.name)
 outfile = fileName + '.csv'
 
-loader.write_csv(filename=outfile, make_strings=args.strings)
+loader.write_csv(filename=outfile, make_strings=args.strings)

Then you have to create a file containing the data in json format, e. g.

$ cat data.json
[
["alpha", "beta", {"one": {"two": "gamma"}}],
["zeta", "eta", {"one": {"two": "theta"}}]
]

...and a file describing the conversion, also in json, like

$ cat key_map.json
{
   "map": [
   ["foo", "0"],
   ["bar", "1"],
   ["baz", "2.one.two"]
   ]
}

Now you can run the script, first to look at the command line help

$ python json2csv.py -h
usage: json2csv.py [-h] [-e] [-o OUTPUT_CSV] [--strings] json_file key_map

Converts JSON to CSV

positional arguments:
  json_file Path to JSON data file to load
  key_map   File containing JSON key-mapping file to load

optional arguments:
  -h, --helpshow this help message and exit
  -e, --each-line   Process each line of JSON file separately
  -o OUTPUT_CSV, --output-csv OUTPUT_CSV
Path to csv file to output
  --strings Convert lists, sets, and dictionaries fully to 
comma-separated strings.

and then to process your data:

$ python json2csv.py data.json key_map.json
INFO:root:[u'alpha', u'beta', {u'one': {u'two': u'gamma'}}]
INFO:root:[u'zeta', u'eta', {u'one': {u'two': u'theta'}}]

Finally, let's have a look at the resulting csv file:

$ cat data.csv
foo,bar,baz
alpha,beta,gamma
zeta,eta,theta


-- 
https://mail.python.org/mailman/listinfo/python-list


Can anybody help me retrieve how to retrieve output from this Python code below!

2017-07-11 Thread ksatish . dtc
try:
import unicodecsv as csv
except ImportError:
import csv

import json
import operator
import os
from collections import OrderedDict
import logging

logging.basicConfig(level=logging.DEBUG)

class Json2Csv(object):
"""Process a JSON object to a CSV file"""
collection = None

# Better for single-nested dictionaries
SEP_CHAR = ', '
KEY_VAL_CHAR = ': '
DICT_SEP_CHAR = '\r'
DICT_OPEN = ''
DICT_CLOSE = ''

# Better for deep-nested dictionaries
# SEP_CHAR = ', '
# KEY_VAL_CHAR = ': '
# DICT_SEP_CHAR = '; '
# DICT_OPEN = '{ '
# DICT_CLOSE = '} '

def __init__(self, outline):
self.rows = []

if not isinstance(outline, dict):
raise ValueError('You must pass in an outline for JSON2CSV to 
follow')
elif 'map' not in outline or len(outline['map']) < 1:
raise ValueError('You must specify at least one value for "map"')

key_map = OrderedDict()
for header, key in outline['map']:
splits = key.split('.')
splits = [int(s) if s.isdigit() else s for s in splits]
key_map[header] = splits

self.key_map = key_map
if 'collection' in outline:
self.collection = outline['collection']

def load(self, json_file):
self.process_each(json.load(json_file))

def process_each(self, data):
"""Process each item of a json-loaded dict
"""
if self.collection and self.collection in data:
data = data[self.collection]

for d in data:
logging.info(d)
self.rows.append(self.process_row(d))

def process_row(self, item):
"""Process a row of json data against the key map
"""
row = {}

for header, keys in self.key_map.items():
try:
row[header] = reduce(operator.getitem, keys, item)
except (KeyError, IndexError, TypeError):
row[header] = None

return row

def make_strings(self):
str_rows = []
for row in self.rows:
str_rows.append({k: self.make_string(val)
 for k, val in row.items()})
return str_rows

def make_string(self, item):
if isinstance(item, list) or isinstance(item, set) or isinstance(item, 
tuple):
return self.SEP_CHAR.join([self.make_string(subitem) for subitem in 
item])
elif isinstance(item, dict):
return self.DICT_OPEN + 
self.DICT_SEP_CHAR.join([self.KEY_VAL_CHAR.join([k, self.make_string(val)]) for 
k, val in item.items()]) + self.DICT_CLOSE
else:
return unicode(item)

def write_csv(self, filename='output.csv', make_strings=False):
"""Write the processed rows to the given filename
"""
if (len(self.rows) <= 0):
raise AttributeError('No rows were loaded')
if make_strings:
out = self.make_strings()
else:
out = self.rows
with open(filename, 'wb+') as f:
writer = csv.DictWriter(f, self.key_map.keys())
writer.writeheader()
writer.writerows(out)


class MultiLineJson2Csv(Json2Csv):
def load(self, json_file):
self.process_each(json_file)

def process_each(self, data, collection=None):
"""Load each line of an iterable collection (ie. file)"""
for line in data:
d = json.loads(line)
if self.collection in d:
d = d[self.collection]
self.rows.append(self.process_row(d))


def init_parser():
import argparse
parser = argparse.ArgumentParser(description="Converts JSON to CSV")
parser.add_argument('json_file', type=argparse.FileType('r'),
help="Path to JSON data file to load")
parser.add_argument('key_map', type=argparse.FileType('r'),
help="File containing JSON key-mapping file to load")
parser.add_argument('-e', '--each-line', action="store_true", default=False,
help="Process each line of JSON file separately")
parser.add_argument('-o', '--output-csv', type=str, default=None,
help="Path to csv file to output")
parser.add_argument(
'--strings', help="Convert lists, sets, and dictionaries fully to 
comma-separated strings.", action="store_true", default=True)

return parser

json_file = input("Type Json input file name: ")

key_map = input("Type Key value : ")

MultiLineJson2Csv(Json2Csv).init_parser()

Json2Csv.load(json_file)


if __name__ == '__main__':
parser = init_parser()
args = parser.parse_args()

key_map = json.load(args.key_map)
loader = None
if args.each_line:
loader = MultiLineJson2Csv(key_map)
else:
loader = Json2Csv(key_map)

loader.load(args.json_file)

outfile = args.output_csv
if outfile is None:
fileName, fileExtension = os.path.splitext(a

Re: anybody help me

2006-02-10 Thread Steven D'Aprano
On Fri, 10 Feb 2006 02:44:41 -0800, Rahul wrote:

> Hi Everybody
> 
> I have some problem in my script. please help me.

I'll do better: I'll help you help yourself.

First step: what is the problem? Saying "I have a problem" and expecting
us to guess what it is will not give good results.

Second step: what is the ENTIRE traceback you get when you run the script?
Do not write it out by memory -- copy and paste it exactly as Python
prints it.

Third step: do you understand what the traceback is telling you? Hint:
Python almost always knows what you've done wrong. You just have to pay
attention.

Fourth step: if you still can't determine the error, don't expect others
to debug a 1000+ line script. Spend some time cutting the script back to
the smallest possible version that still gives you the same error. This
may even help you understand what the error is.


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list