![]() |
Error message in Jupyter Notebook with json - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Error message in Jupyter Notebook with json (/thread-11001.html) |
Error message in Jupyter Notebook with json - diet - Jun-17-2018 Hi, I am new to Python and Jupyter Notebook and I get the error below in Jupyter Notebook while the code works in Spyder: import base64 import json import subprocess 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) Thanks for help
RE: Error message in Jupyter Notebook with json - volcano63 - Jun-17-2018 (Jun-17-2018, 09:37 AM)diet Wrote: Hi,One thing that is dangerous in your code - you override (shadow) global name format . Maybe that is not your issue - but it is a potentially dangerous substitution
RE: Error message in Jupyter Notebook with json - snippsat - Jun-17-2018 The problem is that you have bytes b'something' in dictionary data.JSON format only supports Unicode strings. As mention bye @volcano63 you overwrite format,but also bytes is a used name bye Python.>>> format <built-in function format> >>> bytes <class 'bytes'> >>> # A name that not used give NameError >>> foo Traceback (most recent call last): File "<string>", line 428, in runcode File "<interactive input>", line 1, in <module> NameError: name 'foo' is not definedYou should also show what you call function with,now do i see that in error message that it's png image. Since Base64 encodes bytes to ASCII-only bytes,you can use that codec to decode the data so it can be dumped to JSON. import base64 import json import subprocess def output_image(name, file_format, img_bytes): image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8" image_end = "END_IMAGE_0238jfw08fjsiufhw8frs" data = {} data['name'] = name data['format'] = file_format encoded = data['bytes'] = base64.encodestring(img_bytes) # Fix data['bytes'] = encoded.decode('ascii') print(image_start + json.dumps(data) + image_end) output_image('logo.png', 'png', open("logo.png", "rb").read()) RE: Error message in Jupyter Notebook with json - volcano63 - Jun-17-2018 The question remains about working in Spyder and not working in Jupyter. I have a nagging suspicion that Jupyter is running Python3, and Spyder - Python2. RE: Error message in Jupyter Notebook with json - snippsat - Jun-17-2018 (Jun-17-2018, 03:13 PM)volcano63 Wrote: I have a nagging suspicion that Jupyter is running Python3, and Spyder - Python2Yes i think that to. @diet check from where you run Spyder,for Python 3 Spyder should be run from your Anaconda3\Scripts folder or from Navigator Anaconda3.Do you have a newer download of Anaconda3 Spyder may not be there bye default(may need install), as you see in VS Code Bundled with Anaconda so have they switch to VS Code and do not support Spyder anymore. |