0

I am getting the following error. What does it mean?

AttributeError: 'bool' object has no attribute 'decode'

in code line : writer.writerow({k:v.decode('utf8') for k,v in dictionary.iteritems()})

My code looks like :

import json
import csv

def make_csv(data):
    fname = "try.csv"
    with open(fname,'wb') as outf:
        dic_list = data['bookmarks']
        dictionary =  dic_list[0]
        writer = csv.DictWriter(outf,fieldnames = sorted(dictionary.keys()), restval = "None", extrasaction = 'ignore')
        writer.writeheader()

        for dictionary in dic_list:
            writer.writerow({k:v.decode('utf8') for k,v in dictionary.iteritems()})
    return


def main():
    fil = "readability.json"
    f = open(fil,'rb')
    data = json.loads(f.read())
    print type(data)
    make_csv(data)

The json file looks like :

{ "bookmarks" : [{..},{..} ..... {..}],
  "recommendations" : [{..},{..}...{..}]
} 

where [..] = list and {..} = dictionary

EDIT :

The above problem was solved, But when I ran the above code, The CSV file generated has some discrepancies. Some rows were pasted randomly i.e. under different headers in .csv file. Any suggestions?

Silent Spy
  • 81
  • 5
  • 1
    At some point, the value of v is a boolean (either True or False), so you're trying to do `True.decode()` or `False.decode()`, which doesn't work. I expect this is a duplicate of some existing question. – Joshua Taylor Feb 20 '15 at 21:06
  • I don't understand how is v boolean, at any point. It is string. The value against the key. – Silent Spy Feb 20 '15 at 21:18
  • There's only one place that you're calling decode, and that's in `v.decode('utf8')`. The error says `'bool' object has no attribute 'decode'`. So the value of `v` is probably that bool object, which means that it's either `True` or `False` (since there aren't any other bool objects). – Joshua Taylor Feb 20 '15 at 21:19
  • okay. I looked at the json file. There are some null values which are translated to `False` here. – Silent Spy Feb 20 '15 at 21:29

1 Answers1

2

Somewhere in your readability.json file you have an entry that's a boolean value, like true or false (in JSON), translated to the Python True and False objects.

You should not be using decode() in the first place, however, as json.loads() already produces Unicode values for strings.

Since this is Python 2, you want to encode your data, to UTF-8, instead. Convert your objects to unicode first:

writer.writerow({
    k: unicode(v).encode('utf8')
    for k ,v in dictionary.iteritems()
})

Converting existing Unicode strings to unicode is a no-op, but for integers, floating point values, None and boolean values you'll get a nice Unicode representation that can be encoded to UTF-8:

>>> unicode(True).encode('utf8')
'True'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Could you suggest me a way to read BIG .json file? I could not find any suitable code to help me. My file is 5MB. However, for the sake of running code, I manually deleted a lot of data so that I can parse and see if it works. What can I do? – Silent Spy Feb 20 '15 at 21:31
  • 1
    @SilentSpy: the standard library `json` module can only parse the whole file in one go. See [Iteratively parse JSON file](http://stackoverflow.com/q/20885797) for iterative parsing options. – Martijn Pieters Feb 20 '15 at 21:32
  • @SilentSpy: alternatively, write *separate* JSON objects to the file with newlines as separators. See [Loading and parsing a JSON file in Python](http://stackoverflow.com/q/12451431) for how to read such a file. – Martijn Pieters Feb 20 '15 at 21:33
  • There's an Error in the .csv file parsed. Some rows are randomly pasted beneath columns. How do I sort this? – Silent Spy Feb 20 '15 at 21:50
  • @SilentSpy: you mean there are embedded newlines? Then your original value had those newlines, and the column will be *quoted* to signal to a compliant CSV parser to preserve those newlines. – Martijn Pieters Feb 20 '15 at 21:54
  • @SilentSpy: note that if you have new problems it is better to ask a new question (after due research). Help in comments never can get very far. – Martijn Pieters Feb 20 '15 at 21:54