pythonjsoncbor

TypeError when reading CBOR file


I am working on a project using the CBOR file to contain data. I already install cbor with pip install cbor. But I cannot read it.

This is my code:

import cbor
cbor.loads(r"C:\Users\User\Desktop\project\score.cbor")

Then its return this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\Anaconda3\envs\pyenv\lib\site-packages\cbor\cbor.py", line 263, in loads
    fp = StringIO(data)
TypeError: a bytes-like object is required, not 'str'

How to solve this problem? And is there a way to convert CBOR to JSON file because I find it easier to work with the JSON file in Python.


Solution

  • I still don't know how to solve this problem with cbor, I use cbor2 instead and it works.

    import cbor2
    path = "home/data/score.cbor"
    
    with open(path, 'rb') as fp:
        obj = cbor2.load(fp)
        print(obj)
    

    Output

    {'root': {'probe': 20, 'candidate': 31}, 'tree': [], 'support': []}