👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
Python code to load data from some long complicated JSON file:
with open(filename, "r") as f:
data = json.loads(f.read())
(note: the best code version should be:
with open(filename, "r") as f:
data = json.load(f)
but both exhibit similar behavior)
For many types of JSON error (missing delimiters, incorrect backslashes in strings, etc), this prints a nice helpful message containing the line and column number where the JSON error was found.
However, for other types of JSON error (including the classic "using comma on the last item in a list", but also other things like capitalising true/false), Python"s output is just:
Traceback (most recent call last):
File "myfile.py", line 8, in myfunction
config = json.loads(f.read())
File "c:python27libjson\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "c:python27libjsondecoder.py", line 360, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:python27libjsondecoder.py", line 378, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
For that type of ValueError, how do you get Python to tell you where is the error in the JSON file?
👻 Read also: what is the best laptop for engineering students?
Displaying better error message than "No JSON object could be decoded" __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
Displaying better error message than "No JSON object could be decoded" __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from Displaying better error message than “No JSON object could be decoded”, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Displaying better error message than “No JSON object could be decoded”
- Deutsch Displaying better error message than “No JSON object could be decoded”
- Français Displaying better error message than “No JSON object could be decoded”
- Español Displaying better error message than “No JSON object could be decoded”
- Türk Displaying better error message than “No JSON object could be decoded”
- Русский Displaying better error message than “No JSON object could be decoded”
- Português Displaying better error message than “No JSON object could be decoded”
- Polski Displaying better error message than “No JSON object could be decoded”
- Nederlandse Displaying better error message than “No JSON object could be decoded”
- 中文 Displaying better error message than “No JSON object could be decoded”
- 한국어 Displaying better error message than “No JSON object could be decoded”
- 日本語 Displaying better error message than “No JSON object could be decoded”
- हिन्दी Displaying better error message than “No JSON object could be decoded”
London | 2023-03-25
Thanks for explaining! I was stuck with Displaying better error message than “No JSON object could be decoded” for some hours, finally got it done 🤗. Will get back tomorrow with feedback
Tallinn | 2023-03-25
I was preparing for my coding interview, thanks for clarifying this - Displaying better error message than “No JSON object could be decoded” in Python is not the simplest one. I just hope that will not emerge anymore
Milan | 2023-03-25
json Python module is always a bit confusing 😭 Displaying better error message than “No JSON object could be decoded” is not the only problem I encountered. I am just not quite sure it is the best method