Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: the JSON object must be str, not 'bytes' #44

Closed
femtotrader opened this issue Aug 24, 2015 · 6 comments
Closed

TypeError: the JSON object must be str, not 'bytes' #44

femtotrader opened this issue Aug 24, 2015 · 6 comments

Comments

@femtotrader
Copy link
Contributor

Hello,

I'm just trying your datapackage library and get

In [1]: import datapackage

In [2]: datapkg = datapackage.DataPackage('http://data.okfn.org/data/cpi/')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-21b6afb5084e> in <module>()
----> 1 datapkg = datapackage.DataPackage('http://data.okfn.org/data/cpi/')

//anaconda/lib/python3.4/site-packages/datapackage/datapackage.py in __init__(self, *args, **kwargs)
    100         elif len(args) == 1:
    101             self.base = args[0]
--> 102             descriptor = self.get_descriptor()
    103             super(DataPackage, self).__init__(**descriptor)
    104         else:

//anaconda/lib/python3.4/site-packages/datapackage/datapackage.py in get_descriptor(self)
    588         # Load the descriptor json contents
    589         str_descriptor = descriptor.read()
--> 590         json_descriptor = json.loads(str_descriptor)
    591
    592         # Return the descriptor json contents (as the dict json.load returns

//anaconda/lib/python3.4/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    310     if not isinstance(s, str):
    311         raise TypeError('the JSON object must be str, not {!r}'.format(
--> 312                             s.__class__.__name__))
    313     if s.startswith(u'\ufeff'):
    314         raise ValueError("Unexpected UTF-8 BOM (decode using utf-8-sig)")

TypeError: the JSON object must be str, not 'bytes'

I'm using Anaconda Python 3

Any idea ?

Kind regards

@hozn
Copy link
Contributor

hozn commented Sep 10, 2015

This seems to be just be lacking support for python 3.x. I am running into same issue and will look at making a pull-request for this, as I'd like to use py3.

@trickvi
Copy link
Owner

trickvi commented Sep 10, 2015

Ah thanks @hozn I have not had time to look into this (I have not yet switched over to 3 so I was going to use this ticket to finally do it but I'd really appreciate all help to make sure @femtotrader is not stuck on this for long and won't have to wait for me to move to 3)

@hozn
Copy link
Contributor

hozn commented Sep 10, 2015

Well, the fix was a one-liner, and actually was incorporated in ae7e740 (see linked commit above).

@trickvi
Copy link
Owner

trickvi commented Sep 10, 2015

If I'm not mistaken this has now been fixed thanks to @hozn and published in version 0.5.4.

@femtotrader could you check if this now works for you?

@femtotrader
Copy link
Contributor Author

Thanks it's working fine now with Python 3.4.3

You might also fix print statements in doc

https://github.com/trickvi/datapackage/blob/master/docs/index.rst

>> import datapackage
>>
>> # Note trailing slash is important for data.okfn.org
>> datapkg = datapackage.DataPackage('http://data.okfn.org/data/cpi/')
>>
>> print datapkg.title
Annual Consumer Price Index (CPI)
>> print datapkg.description
Annual Consumer Price Index (CPI) for most countries in the world. Reference year is 2005.
>> # Weird example just to show how to work with data rows
>> print sum([row['CPI'] for row in datapkg.data])
668134.635662

to

>> import datapackage
>>
>> # Note trailing slash is important for data.okfn.org
>> datapkg = datapackage.DataPackage('http://data.okfn.org/data/cpi/')
>>
>> print(datapkg.title)
Annual Consumer Price Index (CPI)
>> print datapkg.description
Annual Consumer Price Index (CPI) for most countries in the world. Reference year is 2005.
>> # Weird example just to show how to work with data rows
>> print(sum([row['CPI'] for row in datapkg.data]))
668134.635662

@pwalsh
Copy link
Contributor

pwalsh commented Sep 11, 2015

As the one responsible for the python 3 port, I'm glad to see it was such a small thing. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants