Discussion:
[Tutor] Attacking this problem (2 parts):JSON object to CSV file
Saran Ahluwalia
2015-06-19 01:44:43 UTC
Permalink
Good Evening,

I have a conundrum regarding JSON objects and converting them to CSV:

*Context*


- I am converting XML files to a JSON object (please see snippet below)
and then finally producing a CSV file. Here is a an example JSON object:


"PAC": {
"Account": [{
"PC": "0",
"CMC": "0",
"WC": "0",
"DLA": "0",
"CN": null,
"FC": {
"Int32": ["0",
"0",
"0",
"0",
"0"]
},
"F": {
"Description": null,
"Code": "0"
}

In general, when I convert any of the files from JSON to CSV, I have been
successful when using the following:


import csv
import json
import sys

def hook(obj):
return obj

def flatten(obj):
for k, v in obj:
if isinstance(v, list):
yield from flatten(v)
else:
yield k, v

if __name__ == "__main__":
with open("somefileneame.json") as f:
data = json.load(f, object_pairs_hook=hook)

pairs = list(flatten(data))

writer = csv.writer(sys.stdout)
header = writer.writerow([k for k, v in pairs])
row = writer.writerow([v for k, v in pairs]) #writer.writerows for any
other iterable object


However with the example JSON object (above) i receive the following error
when applying this function:

ValueError: too many values to unpack

Here are some more samples.


1. "FC": {"Int32": ["0","0","0","0","0","0"]}
2. "PBA": {"Double": ["0","0","0","0","0","0","0","0"]}


3. "PBDD": {
"DateTime": ["1/1/0001
12:00:00 AM",
"1/1/0001 12:00:00 AM",
"1/1/0001 12:00:00 AM",
"1/1/0001 12:00:00 AM",
"1/1/0001 12:00:00 AM",
"1/1/0001 12:00:00 AM",
"1/1/0001 12:00:00 AM",
"1/1/0001 12:00:00 AM"]
},



In the above example, I would like to remove the keys *Int32*, *Double *and
*DateTime*. I am wondering if there is a function or methodology that
would allow
me to remove such nested keys and reassign the new keys to the outer key
(in this case above *FC, PBA *and *PBDD*) as column headers in a CSV and
concatenate all of the values within the list (as corresponding fields).

Also, here is how I strategized my XML to CSV conversion (if this is of any
use):


import xml.etree.cElementTree as ElementTree
from xml.etree.ElementTree import XMLParser
import json
import csv
import tokenize
import token
try:
from collections import OrderedDict
import json
except ImportError:
from ordereddict import OrderedDict
import simplejson as json
import itertools
import six
import string
from csvkit import CSVKitWriter


class XmlListConfig(list):
def __init__(self, aList):
for element in aList:
if element:
# treat like dict
if len(element) == 1 or element[0].tag != element[1].tag:
self.append(XmlDictConfig(element))
# treat like list
elif element[0].tag == element[1].tag:
self.append(XmlListConfig(element))
elif element.text:
text = element.text.strip()
if text:
self.append(text)


class XmlDictConfig(dict):
'''
tree = ElementTree.parse('your_file.xml')
root = tree.getroot()
xmldict = XmlDictConfig(root)
root = ElementTree.XML(xml_string)
xmldict = XmlDictConfig(root)
And then use xmldict for what it is..a dictionary.
'''
def __init__(self, parent_element):
if parent_element.items():
self.update(dict(parent_element.items()))
for element in parent_element:
if element:
# treat like dict - we assume that if the first two tags
# in a series are different, then they are all different.
if len(element) == 1 or element[0].tag != element[1].tag:
aDict = XmlDictConfig(element)
# treat like list - we assume that if the first two tags
# in a series are the same, then the rest are the same.
else:
# here, we put the list in dictionary; the key is the
# tag name the list elements all share in common, and
# the value is the list itself
aDict = {element[0].tag: XmlListConfig(element)}
# if the tag has attributes, add those to the dict
if element.items():
aDict.update(dict(element.items()))
self.update({element.tag: aDict})
# this assumes that if you've got an attribute in a tag,
# you won't be having any text. This may or may not be a
# good idea -- time will tell. It works for the way we are
# currently doing XML configuration files...
elif element.items():
self.update({element.tag: dict(element.items())})
# finally, if there are no child tags and no attributes, extract
# the text
else:
self.update({element.tag: element.text})



def main():

#Lines 88-89stantiate the class Elementree
#and applies the method to recursively traverse from the root node
#XmlDictConfig is instantiated in line 90

with open('C:\\Users\\wynsa2\\Desktop\\Python
Folder\\PCSU\\Trial2_PCSU\\2-Response.xml', 'r', encoding='utf-8') as f:
xml_string = f.read()
xml_string= xml_string.replace('�', '')
root = ElementTree.XML(xml_string)
xmldict = XmlDictConfig(root)
json_str = json.dumps(xmldict, sort_keys=True, indent=4,
separators=(',', ': '))
newly_formatted_data = json.loads(json_str) #encode into JSON
with open('data2.json', 'w') as f: #writing JSON file
json.dump(newly_formatted_data, f)



I hope that I was clear in my description. Thank you all for your help.

Sincerely,
Saran
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-19 09:49:16 UTC
Permalink
Post by Saran Ahluwalia
However with the example JSON object (above) i receive the following error
ValueError: too many values to unpack
Please always include the full error trace not just the final line.

Also, since this is quite an advanced question for a beginners
list, you might want to consider asking on the main Python list too.
However, I suspect we do have enough JSON users here to give
you some help.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-06-19 10:35:28 UTC
Permalink
Post by Alan Gauld
Post by Saran Ahluwalia
However with the example JSON object (above) i receive the following
ValueError: too many values to unpack
Please always include the full error trace not just the final line.
Also, since this is quite an advanced question for a beginners
list, you might want to consider asking on the main Python list too.
However, I suspect we do have enough JSON users here to give
you some help.
There are already many threads on the main list, all started by Saran, with
little progress.

I provided the csv/json snippet in his post, but the goal posts are moving
and I could not obtain a clear description of the problem. In short: he ate
up my goodwill.


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
a***@yahoo.in
2015-06-21 16:19:43 UTC
Permalink
hello, can you guys give me some  advice regarding web development using python languageIs wordpress is easier to create a website or python framework(django or flask)?If then how to create a website User interface i.e of good elegant looking UI??
Post by Alan Gauld
Post by Saran Ahluwalia
However with the example JSON object (above) i receive the following
ValueError: too many values to unpack
Please always include the full error trace not just the final line.
Also, since this is quite an advanced question for a beginners
list, you might want to consider asking on the main Python list too.
However, I suspect we do have enough JSON users here to give
you some help.
There are already many threads on the main list, all started by Saran, with
little progress.

I provided the csv/json snippet in his post, but the goal posts are moving
and I could not obtain a clear description of the problem. In short: he ate
up my goodwill.


_______________________________________________
Tutor maillist  -  ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription op
Alan Gauld
2015-06-21 17:06:10 UTC
Permalink
hello, can you guys give me some advice regarding web development using python
languageIs wordpress is easier to create a website or python framework
If you just want a standard web site with static content or a small
amount of feedback/blogging type activity Wordpress is easier. If you
want to do something outside of Wordpress' normal style and behaviour
then a Python Framework might be worth considering.
But it all depends on how much you need to customize things.
If then how to create a website User interface i.e of good elegant looking UI??
That's entirely down to your HTML/Javascript skills, and good taste.
Python can't really help you there.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-06-21 17:32:43 UTC
Permalink
If you don't know javascript, and want to code your website in
python, you might consider using web2py

http://www.web2py.com/

With web2py a whole lot of things happen automatically for you
more or less 'by magic'. Whether you consider this a really
great thing because you didn't want to have to learn all the
low level details or a really terrible thing because you want
absolute control over the low level details depends on you.

Laura

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-21 19:09:54 UTC
Permalink
Post by Laura Creighton
If you don't know javascript, and want to code your website in
python, you might consider using web2py
http://www.web2py.com/
New one on me, looks interesting having viewed the first 4 videos.

But not sure how it helps with the UI or Javascript?
Its still just Python on the server? Very similar to Flask
but with a nice web based IDE.
But the UI (View) is still HTML/Javascript
- and indeed web2py includes JQuery as a standard toolset.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-06-21 19:50:42 UTC
Permalink
Post by Alan Gauld
Post by Laura Creighton
If you don't know javascript, and want to code your website in
python, you might consider using web2py
http://www.web2py.com/
New one on me, looks interesting having viewed the first 4 videos.
But not sure how it helps with the UI or Javascript?
Its still just Python on the server? Very similar to Flask
but with a nice web based IDE.
But the UI (View) is still HTML/Javascript
- and indeed web2py includes JQuery as a standard toolset.
Maybe I am wrong, but I thought you could write in nothing but
Python and HTML. I am going to start playing with web2py tomorrow,
so I will find out how wrong I was.

Laura

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-21 20:41:02 UTC
Permalink
Post by Laura Creighton
Post by Alan Gauld
Its still just Python on the server? Very similar to Flask
but with a nice web based IDE.
But the UI (View) is still HTML/Javascript
Maybe I am wrong, but I thought you could write in nothing but
Python and HTML. I am going to start playing with web2py tomorrow,
so I will find out how wrong I was.
Like most Python web frameworks the server side is all Python
but the Views(UI ) are in HTML which includes any Javascript
you care to write. Web2py doesn't care about that it just sends
it to the browser which interprets it as usual.

So to write a modern-style web app with responsive UI you
still need to wite it in HTML/Javascript using a combination
of HTML5 and JQuery for the UI interactions/validations.

The connection between the web2py server side bits (in the
controllers) is the use of {{...}} markers within the HTML.
Anything inside the double curlies gets translated to HTML
by web2py, inserted into the View html and the View then
gets and sent out along with the boilerplate in the
layout (html) file.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...