-1

I have got the following .json file that has to be converted into a Python list of lists for further analysis.

Here is how the input looks like:

[{"RFA_2F":4,"RFA_2A":"E","TARGET_B":0,"LASTGIFT":5,"AVGGIFT":7.4814814815,"PEPSTRFL":"X","LASTDATE":9512,"WEALTH_INDEX":3.33,"INCOME":3,"FISTDATE":9001,"NAME":"Reshama Y. Saat"},
{"RFA_2F":4,"RFA_2A":"E","TARGET_B":0,"LASTGIFT":10,"AVGGIFT":6.8125,"PEPSTRFL":"X","LASTDATE":9512,"WEALTH_INDEX":2.60,"INCOME":1,"FISTDATE":8702,"NAME":"Alex P. Singh"},
{"RFA_2F":1,"RFA_2A":"E","TARGET_B":0,"LASTGIFT":11,"AVGGIFT":7.6428571429,"PEPSTRFL":"X","LASTDATE":9504,"WEALTH_INDEX":5.14,"INCOME":4,"FISTDATE":8701,"NAME":"Vamsee M. Doban"},...]

The code I am currently using:

import json
f = open('assess2_data.json')
d = json.load(f)

training = []
for row in d:
    for field in row:
        row = []
        row.append([field])
    training.append(row)

I know there is an error, but I cannot think of a way to address it:

TypeError: list indices must be integers, not unicode

The output should look like:

[[4,"E",0,5,7.4814814815,"X",9512,3.33,3,9001,"Reshama Y. Saat"],...]
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Maiia S.
  • 313
  • 6
  • 12

1 Answers1

1

The json file is simple,so you can just get values by using dict.values()

import json
with open('test.json') as f:
    d = json.load(f)
    print [row.values() for row in d]
McGrady
  • 10,869
  • 13
  • 47
  • 69
  • Thank you for a quick response, @McGrady! Is there a way to get actual strings instead of the unicodes? – Maiia S. Mar 07 '17 at 04:36
  • Try `str()` method,have a look at [Python string prints as u'String'](http://stackoverflow.com/questions/599625/python-string-prints-as-ustring) and [How to turn Unicode strings into regular strings](http://stackoverflow.com/questions/4855645/how-to-turn-unicode-strings-into-regular-strings) – McGrady Mar 07 '17 at 04:46