json.loads(x) works, but json.load(frr) doesn't work. Data are the same.
Any idea?
>>> frr = open("jsdata.json", "r")
>>> x = frr.read()
>>> json.loads(x)
{'value_d1': [0, 1, 2, 3, 4, 5, 6, 7, 8], 'dtime': '00.00.00 00:00', 'value_d2': [0, 1, 2, 3, 4, 5, 6, 7,
8], 'device_2': 'Humidity', 'device_1': 'Temperature'}
>>> fr = json.load(frr)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: syntax error in JSON
json.load
-
- Posts: 89
- Joined: Fri Sep 11, 2015 10:47 pm
- Location: Italy
Re: json.load
Hi Karebo Try with:
and with:
the problem is in the position of the cursor in the file.
If you use frr.read() the cursor is positioned at the end of the file and using json.load(frr) there is nothing else to read.
using the with you ensure a correct reading of the file and closing.
If you want to use the same FD then use the seek method.
Code: Select all
with open("jsdata.json", "r") as frr:
x = frr.read()
json.loads(x)
Code: Select all
with open("jsdata.json", "r") as frr:
json.load(frr)
If you use frr.read() the cursor is positioned at the end of the file and using json.load(frr) there is nothing else to read.
using the with you ensure a correct reading of the file and closing.
If you want to use the same FD then use the seek method.
Re: json.load
Thanks. My live will be better now