Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92771
| X-FeedAbuse | http://nntpfeed.proxad.net/abuse.pl feeded by 78.192.65.63 |
|---|---|
| Path | csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!news.muarf.org!news.roellig-ltd.de!open-news-network.org!border2.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'value,': 0.03; 'ideally': 0.04; 'python3': 0.05; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'column': 0.07; 'json': 0.07; 'null,': 0.07; 'subject:file': 0.07; 'wednesday,': 0.07; 'iterate': 0.09; 'obj': 0.09; 'parsed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:a 80': 0.09; 'python': 0.11; 'def': 0.14; 'file,': 0.15; 'skip:f 30': 0.15; '"2"': 0.16; 'apologies:': 0.16; 'csv': 0.16; 'list):': 0.16; 'names?': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:CSV': 0.16; 'subject:Object': 0.16; 'writer': 0.16; 'wrote:': 0.16; 'duplicate': 0.18; 'keys': 0.22; '2015': 0.23; 'advance.': 0.23; 'originally': 0.23; 'file.': 0.24; 'import': 0.24; 'written': 0.24; 'header': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'xml': 0.27; 'yield': 0.27; 'be:': 0.29; 'cat': 0.29; 'key,': 0.29; 'values': 0.30; 'included': 0.32; 'null': 0.33; 'windows.': 0.33; '(for': 0.34; 'editor': 0.34; 'this?': 0.34; 'file': 0.34; 'to:addr:python- list': 0.35; 'follows:': 0.35; 'really': 0.35; 'skip:o 20': 0.35; 'but': 0.36; 'text': 0.36; 'there': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'associated': 0.38; 'received:org': 0.38; 'thank': 0.39; 'application': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'data': 0.40; 'received:de': 0.40; 'forget': 0.60; '3.4': 0.84; 'confusion.': 0.84; 'excel,': 0.84; 'sublime': 0.84; 'utc-4,': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: JSON Object to CSV file |
| Date | Wed, 17 Jun 2015 20:19:46 +0200 |
| Organization | None |
| References | <dd791644-35b4-4400-a683-d409808c0c5b@googlegroups.com> <5747bb7f-04c0-4097-8c09-1102499b49b2@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd8de7.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.561.1434565217.13271.python-list@python.org> (permalink) |
| Lines | 145 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1434565217 news.xs4all.nl 2926 [2001:888:2000:d::a6]:32903 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:92771 |
Show key headers only | View raw
Sahlusar wrote:
> On Wednesday, June 17, 2015 at 11:00:24 AM UTC-4, Saran A wrote:
>> I would like to have this JSON object written out to a CSV file so that
>> the keys are header fields (for each of the columns) and the values are
>> values that are associated with each header field. Is there a best
>> practice for working with this? Ideally I would like to recursively
>> iterate through the key value pairs. Thank you in advance. I am using
>> Python 3.4 on Windows. My editor is Sublime 2.
>>
>> {
>> "CF": {
>> "A": "5",
>> "FEC": "1/1/0001 12:00:00 AM",
>> "TE": null,
>> "Locator": null,
>> "Message": "Transfer Fee",
>> "AT": null,
>> "FT": null,
>> "FR": "True",
>> "FY": null,
>> "FR": null,
>> "FG": "0",
>> "Comment": null,
>> "FUD": null,
>> "cID": null,
>> "GEO": null,
>> "ISO": null,
>> "TRID": null,
>> "XTY": "931083",
>> "ANM": null,
>> "NM": null
>> },
>> "CF": "Fee",
>> "ID": "2"
>> }
>
> My apologies:
>
> I originally parsed this from an XML file.
>
> It really should be:
>
> {
> "Fee": {
> "A": "5",
> "FEC": "1/1/0001 12:00:00 AM",
> "TE": null,
> "Locator": null,
> "Message": "Transfer Fee",
> "AT": null,
> "FT": null,
> "FR": "True",
> "FY": null,
> "FR": null,
> "FG": "0",
> "Comment": null,
> "FUD": null,
> "cID": null,
> "GEO": null,
> "ISO": null,
> "TRID": null,
> "XTY": "931083",
> "ANM": null,
> "NM": null
> },
> "CF": "Fee",
> "ID": "2"
> }
>
> The value, "Fee" associated with the key, "CF" does should not be included
> as a column header.
>
> The CSV file, when opened with an application such as MS Excel, should be
> as follows:
>
> (Column Header)----> CF A FEC
>
> (Field Value)----> Fee 5 1/1/0001 12:00:00 AM
>
> My apologies for an confusion.
Forget about Excel, what should the CSV look like when opened in a text
editor?
Here's my guess:
$ cat input.json
{
"Fee": {
"A": "5",
"FEC": "1/1/0001 12:00:00 AM",
"TE": null,
"Locator": null,
"Message": "Transfer Fee",
"AT": null,
"FT": null,
"FR": "True",
"FY": null,
"FR": null,
"FG": "0",
"Comment": null,
"FUD": null,
"cID": null,
"GEO": null,
"ISO": null,
"TRID": null,
"XTY": "931083",
"ANM": null,
"NM": null
},
"CF": "Fee",
"ID": "2"
}
$ cat json2csv.py
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("input.json") as f:
data = json.load(f, object_pairs_hook=hook)
pairs = list(flatten(data))
writer = csv.writer(sys.stdout)
writer.writerow([k for k, v in pairs])
writer.writerow([v for k, v in pairs])
$ python3 json2csv.py
A,FEC,TE,Locator,Message,AT,FT,FR,FY,FR,FG,Comment,FUD,cID,GEO,ISO,TRID,XTY,ANM,NM,CF,ID
5,1/1/0001 12:00:00 AM,,,Transfer Fee,,,True,,,0,,,,,,,931083,,,Fee,2
But do you really want duplicate column names?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
JSON Object to CSV file Saran A <ahlusar.ahluwalia@gmail.com> - 2015-06-17 08:00 -0700
Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-17 08:45 -0700
Re: JSON Object to CSV file Peter Otten <__peter__@web.de> - 2015-06-17 20:19 +0200
Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-17 13:20 -0700
Re: JSON Object to CSV file Denis McMahon <denismfmcmahon@gmail.com> - 2015-06-21 06:45 +0000
Re: JSON Object to CSV file sahluwalia@wynyardgroup.com - 2015-06-21 06:57 -0700
Re: JSON Object to CSV file Denis McMahon <denismfmcmahon@gmail.com> - 2015-06-21 20:52 +0000
Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-21 16:38 -0700
Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-21 07:22 -0700
Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-21 07:54 -0700
Re: JSON Object to CSV file Ned Batchelder <ned@nedbatchelder.com> - 2015-06-21 08:31 -0700
Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-21 08:39 -0700
Re: JSON Object to CSV file ryguy7272 <ryanshuell@gmail.com> - 2015-08-19 18:54 -0700
csiph-web