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?

4
  • 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. Commented Feb 20, 2015 at 21:06
  • I don't understand how is v boolean, at any point. It is string. The value against the key. Commented Feb 20, 2015 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). Commented Feb 20, 2015 at 21:19
  • okay. I looked at the json file. There are some null values which are translated to False here. Commented Feb 20, 2015 at 21:29

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

6 Comments

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?
@SilentSpy: the standard library json module can only parse the whole file in one go. See Iteratively parse JSON file for iterative parsing options.
@SilentSpy: alternatively, write separate JSON objects to the file with newlines as separators. See Loading and parsing a JSON file in Python for how to read such a file.
There's an Error in the .csv file parsed. Some rows are randomly pasted beneath columns. How do I sort this?
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.