Python Forum
Python 3.5.2 & json on win 10
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3.5.2 & json on win 10
#1
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]}
Reply
#2
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.
Reply
#3
(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?
Reply
#4
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]]}
Reply
#5
Are you running Win 10 and python 3.5.2?
Reply
#6
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.
Reply
#7
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???
Reply
#8
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.
Reply
#9
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.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Split json into separate json based on node value CzarR 1 5,471 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  difficulties to chage json data structure using json module in python Sibdar 1 2,043 Apr-03-2020, 06:47 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020