Python errors in file encode
this is my code:
prettyPicture(clf, features_test, labels_test)
output_image("F:/test.png", "png", open("F:/test.png", "rb").read())
def output_image(name, format, bytes):
image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8"
image_end = "END_IMAGE_0238jfw08fjsiufhw8frs"
data = {}
data['name'] = name
data['format'] = format
data['bytes'] = base64.encodestring(bytes)
print(image_start + json.dumps(data) + image_end)
this errors is:
Traceback (most recent call last):
File "studentMain.py", line 41, in <module>
output_image("F:/test.png", "png", open("F:/test.png", "rb").read())
File "F:Democlass_vis.py", line 69, in output_image
print(image_start + json.dumps(data) + image_end)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjson__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable
python encode
add a comment |
this is my code:
prettyPicture(clf, features_test, labels_test)
output_image("F:/test.png", "png", open("F:/test.png", "rb").read())
def output_image(name, format, bytes):
image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8"
image_end = "END_IMAGE_0238jfw08fjsiufhw8frs"
data = {}
data['name'] = name
data['format'] = format
data['bytes'] = base64.encodestring(bytes)
print(image_start + json.dumps(data) + image_end)
this errors is:
Traceback (most recent call last):
File "studentMain.py", line 41, in <module>
output_image("F:/test.png", "png", open("F:/test.png", "rb").read())
File "F:Democlass_vis.py", line 69, in output_image
print(image_start + json.dumps(data) + image_end)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjson__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable
python encode
1
The error tells you exactly what's wrong. Your dict contains abytesobject, whichjson.dumps()can't serialize. How you resolve that depends on what exactly you need.
– glibdud
Nov 20 '18 at 15:08
1
you can look here: link, there is a good solution
– nerd100
Nov 20 '18 at 15:11
add a comment |
this is my code:
prettyPicture(clf, features_test, labels_test)
output_image("F:/test.png", "png", open("F:/test.png", "rb").read())
def output_image(name, format, bytes):
image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8"
image_end = "END_IMAGE_0238jfw08fjsiufhw8frs"
data = {}
data['name'] = name
data['format'] = format
data['bytes'] = base64.encodestring(bytes)
print(image_start + json.dumps(data) + image_end)
this errors is:
Traceback (most recent call last):
File "studentMain.py", line 41, in <module>
output_image("F:/test.png", "png", open("F:/test.png", "rb").read())
File "F:Democlass_vis.py", line 69, in output_image
print(image_start + json.dumps(data) + image_end)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjson__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable
python encode
this is my code:
prettyPicture(clf, features_test, labels_test)
output_image("F:/test.png", "png", open("F:/test.png", "rb").read())
def output_image(name, format, bytes):
image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8"
image_end = "END_IMAGE_0238jfw08fjsiufhw8frs"
data = {}
data['name'] = name
data['format'] = format
data['bytes'] = base64.encodestring(bytes)
print(image_start + json.dumps(data) + image_end)
this errors is:
Traceback (most recent call last):
File "studentMain.py", line 41, in <module>
output_image("F:/test.png", "png", open("F:/test.png", "rb").read())
File "F:Democlass_vis.py", line 69, in output_image
print(image_start + json.dumps(data) + image_end)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjson__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:UsersTonyAppDataLocalProgramsPythonPython36-
32libjsonencoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable
python encode
python encode
asked Nov 20 '18 at 15:01
liangpu597liangpu597
142
142
1
The error tells you exactly what's wrong. Your dict contains abytesobject, whichjson.dumps()can't serialize. How you resolve that depends on what exactly you need.
– glibdud
Nov 20 '18 at 15:08
1
you can look here: link, there is a good solution
– nerd100
Nov 20 '18 at 15:11
add a comment |
1
The error tells you exactly what's wrong. Your dict contains abytesobject, whichjson.dumps()can't serialize. How you resolve that depends on what exactly you need.
– glibdud
Nov 20 '18 at 15:08
1
you can look here: link, there is a good solution
– nerd100
Nov 20 '18 at 15:11
1
1
The error tells you exactly what's wrong. Your dict contains a
bytes object, which json.dumps() can't serialize. How you resolve that depends on what exactly you need.– glibdud
Nov 20 '18 at 15:08
The error tells you exactly what's wrong. Your dict contains a
bytes object, which json.dumps() can't serialize. How you resolve that depends on what exactly you need.– glibdud
Nov 20 '18 at 15:08
1
1
you can look here: link, there is a good solution
– nerd100
Nov 20 '18 at 15:11
you can look here: link, there is a good solution
– nerd100
Nov 20 '18 at 15:11
add a comment |
2 Answers
2
active
oldest
votes
The issue here is that base64.encodestring() returns a bytes object, not a string.
Try:
data['bytes'] = base64.encodestring(bytes).decode('ascii')
Check out this question and answer for a good explanation of why this is:
Why does base64.b64encode() return a bytes object?
Also see: How to encode bytes in JSON? json.dumps() throwing a TypeError
add a comment |
You're only missing one aspect here: When you use .encodestring, you have a bytes object as result, and bytes are not json serializable in python 3.
You can solve it just encoding your data["bytes"]:
data['bytes'] = base64.encodestring(bytes).decode("utf-8")
I'm assuming you'll always receive a bytes object at the "bytes" variable, otherwise you should add a checker for the type of the object, and not encoding when it's already a string.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395812%2fpython-errors-in-file-encode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue here is that base64.encodestring() returns a bytes object, not a string.
Try:
data['bytes'] = base64.encodestring(bytes).decode('ascii')
Check out this question and answer for a good explanation of why this is:
Why does base64.b64encode() return a bytes object?
Also see: How to encode bytes in JSON? json.dumps() throwing a TypeError
add a comment |
The issue here is that base64.encodestring() returns a bytes object, not a string.
Try:
data['bytes'] = base64.encodestring(bytes).decode('ascii')
Check out this question and answer for a good explanation of why this is:
Why does base64.b64encode() return a bytes object?
Also see: How to encode bytes in JSON? json.dumps() throwing a TypeError
add a comment |
The issue here is that base64.encodestring() returns a bytes object, not a string.
Try:
data['bytes'] = base64.encodestring(bytes).decode('ascii')
Check out this question and answer for a good explanation of why this is:
Why does base64.b64encode() return a bytes object?
Also see: How to encode bytes in JSON? json.dumps() throwing a TypeError
The issue here is that base64.encodestring() returns a bytes object, not a string.
Try:
data['bytes'] = base64.encodestring(bytes).decode('ascii')
Check out this question and answer for a good explanation of why this is:
Why does base64.b64encode() return a bytes object?
Also see: How to encode bytes in JSON? json.dumps() throwing a TypeError
answered Nov 20 '18 at 15:12
Hal JarrettHal Jarrett
441411
441411
add a comment |
add a comment |
You're only missing one aspect here: When you use .encodestring, you have a bytes object as result, and bytes are not json serializable in python 3.
You can solve it just encoding your data["bytes"]:
data['bytes'] = base64.encodestring(bytes).decode("utf-8")
I'm assuming you'll always receive a bytes object at the "bytes" variable, otherwise you should add a checker for the type of the object, and not encoding when it's already a string.
add a comment |
You're only missing one aspect here: When you use .encodestring, you have a bytes object as result, and bytes are not json serializable in python 3.
You can solve it just encoding your data["bytes"]:
data['bytes'] = base64.encodestring(bytes).decode("utf-8")
I'm assuming you'll always receive a bytes object at the "bytes" variable, otherwise you should add a checker for the type of the object, and not encoding when it's already a string.
add a comment |
You're only missing one aspect here: When you use .encodestring, you have a bytes object as result, and bytes are not json serializable in python 3.
You can solve it just encoding your data["bytes"]:
data['bytes'] = base64.encodestring(bytes).decode("utf-8")
I'm assuming you'll always receive a bytes object at the "bytes" variable, otherwise you should add a checker for the type of the object, and not encoding when it's already a string.
You're only missing one aspect here: When you use .encodestring, you have a bytes object as result, and bytes are not json serializable in python 3.
You can solve it just encoding your data["bytes"]:
data['bytes'] = base64.encodestring(bytes).decode("utf-8")
I'm assuming you'll always receive a bytes object at the "bytes" variable, otherwise you should add a checker for the type of the object, and not encoding when it's already a string.
answered Nov 20 '18 at 15:32
Luan NaufalLuan Naufal
5008
5008
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395812%2fpython-errors-in-file-encode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The error tells you exactly what's wrong. Your dict contains a
bytesobject, whichjson.dumps()can't serialize. How you resolve that depends on what exactly you need.– glibdud
Nov 20 '18 at 15:08
1
you can look here: link, there is a good solution
– nerd100
Nov 20 '18 at 15:11