Posts: 50
Threads: 14
Joined: Nov 2016
I have uninstalled python on my win 10 system and re-installed python 3.5.2. The following tiny bit of code causes a series of error messages
import json
with open('schedule.json') as data_file:
data = json.load(data_file)
print(data)
close("schedule.json") here is the error message:
Error: Traceback (most recent call last):
File "read_json.py", line 4, in <module>
data = json.load(data_file)
File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
------------------
(program exited with code: 1)
Press any key to continue . . .
And finally, here is the schedule.json file:
{"zones": [[1, 3], [3, 5], [5, 89]], "weekdays": [1, 2], "ontime": [5, 1]}
Posts: 2,342
Threads: 62
Joined: Sep 2016
How are you running your code? I can't reproduce the problem. That said, your last line can be safely removed; the file is closed with the with block automatically.
Posts: 50
Threads: 14
Joined: Nov 2016
(Nov-28-2016, 12:04 AM)micseydel Wrote: How are you running your code? I can't reproduce the problem. That said, your last line can be safely removed; the file is closed with the with block automatically.
I am running from geany. I just tried running it from a command prompt withy python read_json.py and got the same result
Are you getting an output?
Posts: 2,342
Threads: 62
Joined: Sep 2016
Here's a terminal session for me on Linux
$ cat schedule.json
{"zones": [[1, 3], [3, 5], [5, 89]], "weekdays": [1, 2], "ontime": [5, 1]}
$ cat testit.py
import json
with open('schedule.json') as data_file:
data = json.load(data_file)
print(data)
$ python testit.py
{u'zones': [[1, 3], [3, 5], [5, 89]], u'weekdays': [1, 2], u'ontime': [5, 1]}
$ python3 testit.py
{'ontime': [5, 1], 'weekdays': [1, 2], 'zones': [[1, 3], [3, 5], [5, 89]]}
Posts: 50
Threads: 14
Joined: Nov 2016
Are you running Win 10 and python 3.5.2?
Posts: 2,342
Threads: 62
Joined: Sep 2016
No, I'm using Linux, and I tried Python 2 and Python 3.4. Python and JSON should both be platform independent though, so I don't think that's the problem. My best guess at this point is that you actually have an empty file, and you have an editor window open with unsaved changes.
If that's not the case, try running the code
import json
with open('schedule.json') as data_file:
print("<>".format(data_file.read())) # print out the contents of the file, within <>
data_file.seek(0) # restore file state so that the below line might work
data = json.load(data_file)
print(data) You can also try creating a Git repo or something that we can clone and know is reproducing the problem for you, since describing it over the forum might not be enough in this case.
Posts: 50
Threads: 14
Joined: Nov 2016
I get a different error running the code you provided.
Here is the error message I got an a Debian Linux system:
Error: pi@sprinklersystem:~ $ python test.py
<>
Traceback (most recent call last):
File "test.py", line 5, in <module>
data = json.load(data_file)
NameError: name 'json' is not defined
I'm afraid I don't understand what "Git repo or something that we can clone" is???
Posts: 2,342
Threads: 62
Joined: Sep 2016
It looks like you didn't include the import statement. that said, the "<>" line does indicate that the problem is that the .json file is empty.
(Nov-28-2016, 04:16 AM)PickyBiker Wrote: I'm afraid I don't understand what "Git repo or something that we can clone" is??? Basically a nerdier version of zip files. I'm quite confident at this point that the issue is an empty JSON file, but if you're still having issues, create a zip file with everything needed to reproduce the problem and we can try to use that.
Posts: 7,324
Threads: 123
Joined: Sep 2016
I have testet with win-10 Python 3.5.2 no problem.
In most cases your:
Error: json.loads- JSONDecodeError: Expecting value: line 1 column 1 (char 0)
- non-JSON conforming quoting
- XML/HTML output (that is, a string starting with <), or
- incompatible character encoding
To make sure your json file is correct,
paste date in Json Formatter --> Process,
now download file and try with this file.
Posts: 50
Threads: 14
Joined: Nov 2016
Okay, here is the latest status
The schedule.json file is okay according to the online json formatter.
The simpler code now is:
import json
with open('schedule.json') as data_file:
data = json.load(data_file)
print(data) The code and json file work just fine on a debian system.
And it still fails with Win 10 system
Error: Traceback (most recent call last):
File "read_json.py", line 4, in <module>
data = json.load(data_file)
File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Michael\AppData\Local\Programs\Python\Python35\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
------------------
(program exited with code: 1)
Press any key to continue . . .
It looks to me that this problem is unique to win 10.
|