TypeError when reading CBOR file - python

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.

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': []}

Related

TypeError while loading JSON in Python

This is my JSON file:
{
"pass": "test",
"users": "joel"
}
And this is my Python code:
with open("auth.json", "r") as f:
data = json.load(f)
This is error I am getting:
Traceback (most recent call last):
File "W:/ComputerScienceDiceGame/dice.py", line 21, in
data = json.load(f.read())
File "C:\Python33\lib\json__init__.py", line 268, in load
return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
I know this is probably easy but I can't find the root cause of the issue.
Pick one:
data = json.load(f)
data = json.loads(f.read())
The point here is, .load loads JSON from a reader (file descriptor), while .loads loads JSON from a string that's already read into memory/buffer.

convert json retrived from url

I am partially able to work with json saved as file:
#! /usr/bin/python3
import json
from pprint import pprint
json_file='a.json'
json_data=open(json_file)
data = json.load(json_data)
json_data.close()
print(data[10])
But I am trying to achieve the same from data directly from web. I am trying with the accepted answer here:
#! /usr/bin/python3
from urllib.request import urlopen
import json
from pprint import pprint
jsonget=urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee")
data = json.load(jsonget)
pprint(data)
which is giving me error:
Traceback (most recent call last):
File "i.py", line 10, in <module>
data = json.load(jsonget)
File "/usr/lib64/python3.5/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib64/python3.5/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
What is going wrong here?
Changing the code as par Charlie's reply to:
jsonget=str(urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee"))
data = json.load(jsonget)
pprint(jsonget)
breaks at json.load:
Traceback (most recent call last):
File "i.py", line 9, in <module>
data = json.load(jsonget)
File "/usr/lib64/python3.5/json/__init__.py", line 265, in load
return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
It's actually telling you the answer: you're getting back a byte array, where in Python 3 a string is different because of dealing with unicode. In Python 2.7, it would work. You should be able to fix it by converting your bytes explicitly to a string with
jsonget=str(urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee")_

How do I save a file object to a shelve file?

When I run the following code:
import shelve
input = open("input.txt",)
shelveFile = shelve.open("myData")
shelveFile["inputFile"] = input
input.close()
shelveFile.close()
I expect the shelve file myData to hold the file object input. Instead, running the code produces the following error:
Traceback (most recent call last):
File "/Users/ashutoshmishra/Documents/Sandbox/Sandbox3.py", line 5, in <module>
shelveFile["inputFile"] = input
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shelve.py", line 124, in __setitem__
p.dump(value)
TypeError: cannot serialize '_io.TextIOWrapper' object
I was wondering why I could not save the file object input to the shelve file myData?
The following answer is taken from #DanD.'s comment above.
Read the file: shelveFile["inputFile"] = input.read()

tarfile.addfile gives error: TypeError: 'str' does not support the buffer interface

This is my first post about Python, and I'm kind of new to it.
Unfortunately I need to use Windows as my OS.
I'm using python 3.3.2 from python.org.
I'm trying to create a tar file from memory, adding a FILE.txt to it.
from io import StringIO
import tarfile
archive_files = []
data = ["DATA1 "]
data.append("DATA2 ")
archive_files.append(("FILE.txt", "\n".join(data)))
tar = tarfile.open ("file.tar", "w:tar")
for name, data in archive_files:
info = tarfile.TarInfo(name)
info.size = len(data)
tar.addfile(info, StringIO(data))
tar.close()
This is the error:
Traceback (most recent call last):
File "<string>", line 420, in run_nodebug
File "C:\home\rs94036\src\python\testTar.py", line 14, in <module>
tar.addfile(info, StringIO(data))
File "C:\Python33\lib\tarfile.py", line 1957, in addfile
copyfileobj(fileobj, self.fileobj, tarinfo.size)
File "C:\Python33\lib\tarfile.py", line 274, in copyfileobj
dst.write(buf)
TypeError: 'str' does not support the buffer interface
I know my problem is in the line 14, but I can't understand what's wrong.
This code is used in Linux, specifically in pacman (ArchLinux package manager automated tests).
Can someone help me?
Thanks,
Renan
You have to write bytes to a tar file, not str, so you have to encode your data before you write it to a tar file. (Encoding of your choice, I used utf-8 in the example)
Problematic lines:
info.size = len(data)
tar.addfile(info, StringIO(data))
Should be something like:
encoded = data.encode('utf-8')
info.size = len(encoded)
tar.addfile(info, BytesIO(encoded))

Python pickle syntax

I'm using the latest python release and after searching, I can't seem to find anything on pickles that will work for me.
I am simply going through tutorials attempting to learn about pickling and none of the source code that apparently works on the tutorials will work for me, I suspect this is something to do with the tutorials being outdated.
What I have tried and is the same as what tutorials show is:
import pickle
lists = [1,2,3,4,5]
pickle.dump(lists, open('log.txt', 'a+'))
which gives me the following error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
pickle.dump(lists, open('log.txt', 'a+'))
TypeError: must be str, not bytes
this
>>> import pickle
>>> unpicklefile = open('log.txt', 'r')
>>> unpickledlist = [1,2,3,4,5]
>>> unpickledlist = pickle.load(unpicklefile)
gives me the following error:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
unpickledlist = pickle.load(unpicklefile)
TypeError: 'str' does not support the buffer interface
Thank you for any replies and help
The 'a+' mode may be causing you problems. And, if you're on Windows, it would be useful to open a file in a binary mode. Also, you should close the file before reopening to read it back in. And make sure you're writing and reading the same file ('log.txt' vs. 'filename'):
import pickle
lists = [1,2,3,4,5]
f = open('tmp_pickle.pic', 'wb')
pickle.dump(lists, f)
f.close()
f = open('tmp_pickle.pic', 'rb')
unpickledlist = pickle.load(f)
print unpickledlist

Categories