Bitcoin Forum
November 12, 2024, 02:14:45 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: json-to-csv.py: a handy tool for wallet transaction analysis  (Read 4163 times)
mikegogulski (OP)
Sr. Member
****
Offline Offline

Activity: 360
Merit: 250



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
Gavin Andresen
Legendary
*
qt
Offline Offline

Activity: 1652
Merit: 2301


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 (OP)
Sr. Member
****
Offline Offline

Activity: 360
Merit: 250



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:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!