Bitcoin Forum
January 20, 2017, 12:04:36 PM *
News: Latest stable version of Bitcoin Core: 0.13.2  [Torrent]. (New!)
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: json-to-csv.py: a handy tool for wallet transaction analysis  (Read 3809 times)
mikegogulski
Sr. Member
****
Offline Offline

Activity: 360



View Profile WWW
June 08, 2011, 04:49:26 PM
 #1

I'm sure somebody has rolled this up before, but I had cause to today.

Usage:
 bitcoind listtransactions '*' 999999 | python json-to-csv.py > transactions.csv
 Use favorite spreadsheet program to open transactions.csv

Code:
#!/usr/bin/python

import json
import sys

input = sys.stdin.read()
data = json.loads(input)

keys = []

for record in data:
for key in record:
if key not in keys:
keys.append(key)

cur = ""
output = ""

for key in keys:
output += '"' + key + '",'
output += '\n'

for record in data:
for key in keys:
if key in record:
if record[key].__class__ is int or record[key].__class__ is float:
output += str(record[key])
else:
output += '"' + record[key] + '"'
output += ','
output += '\n'

print output

FREE ROSS ULBRICHT, allegedly one of the Dread Pirates Roberts of the Silk Road
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1484913876
Hero Member
*
Offline Offline

Posts: 1484913876

View Profile Personal Message (Offline)

Ignore
1484913876
Reply with quote  #2

1484913876
Report to moderator
1484913876
Hero Member
*
Offline Offline

Posts: 1484913876

View Profile Personal Message (Offline)

Ignore
1484913876
Reply with quote  #2

1484913876
Report to moderator
1484913876
Hero Member
*
Offline Offline

Posts: 1484913876

View Profile Personal Message (Offline)

Ignore
1484913876
Reply with quote  #2

1484913876
Report to moderator
Gavin Andresen
Legendary
*
qt
Offline Offline

Activity: 1652


Chief Scientist


View Profile WWW
June 08, 2011, 11:46:56 PM
 #2

I think I meant to post my version and just got busy and forgot...
Quote
#!/usr/bin/env python
#
# Reads an array of JSON objects and writes out CSV-format,
# with key names in first row.
# Columns will be union of all keys in the objects.
#

import csv
import json
import sys

json_string = sys.stdin.read()
json_array = json.loads(json_string)

columns = set()
for item in json_array:
  columns.update(set(item))

writer = csv.writer(sys.stdout)
writer.writerow(list(columns))
for item in json_array:
  row = []
  for c in columns:
    if c in item: row.append(str(item[c]))
    else: row.append('')
  writer.writerow(row)

How often do you get the chance to work on a potentially world-changing project?
mikegogulski
Sr. Member
****
Offline Offline

Activity: 360



View Profile WWW
June 09, 2011, 12:59:57 AM
 #3

I think I meant to post my version and just got busy and forgot...

Oh. There's a python CSV module. *facepalm*. Way sexier, sir!

FREE ROSS ULBRICHT, allegedly one of the Dread Pirates Roberts of the Silk Road
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!