[Tutor] using json to pass a dict thru a file
Doug Basberg
dbasberg at comcast.net
Tue Mar 17 23:30:03 CET 2015
I appreciate the advise to use json to pass a dict thru a file. Below is
the code
To 'dump' the dict to a file and the code to 'load' the dict and the error
message
I get testing this.
What am I doing wrong? Thanks.
////////////////////////////////////////////////////////////////////////////
//
///// code to create the file with json
/////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//
#! /usr/bin/python
############################################################################
##
# file = myTest00.py
# try out concepts for use in 5500 Power project
#
############################################################################
##
##### Module history
#######################################################
############################################################################
##
#
import time as t
import json
tymNow = t.time()
tymLocal = t.localtime()
tymAsc = t.asctime(tymLocal)
dataDict = {'Time': None, 'Vbatt': None, 'Ichrg': None, 'Vpanel': None}
vb = 51.25
ic = 5.89
vp = 55.78
i = 0
dataDict['Time'] = tymAsc
dataDict['Vbatt'] = vb
dataDict['Ichrg'] = ic
dataDict['Vpanel'] = vp
fn = 'testData01.dat'
f = open(fn, 'ab')
for i in xrange(5):
json.dump(dataDict, f)
# f.write(str(dataDict))
# f.write('\n\r')
tymAsc = t.asctime(t.localtime())
dataDict['Time'] = tymAsc
dataDict['Vbatt'] = vb + i
dataDict['Ichrg'] = ic + i
dataDict['Vpanel'] = vp + i
f.close()
print('*********** done ********** \n\r')
////////////////////////////////////////////////////////////////////////////
//
///// code to use json to load the dict from the file
//////////////////////
////////////////////////////////////////////////////////////////////////////
//
#! /usr/bin/python
############################################################################
##
# file = myTestRcv00.py
# try out concepts for use in 5500 Power project
#
############################################################################
##
##### Module history
#######################################################
############################################################################
##
#
import json
#dataDict = {}
fn = 'testData01.dat'
f = open(fn, 'r')
dataDict = json.load(f)
f.close()
print(type(dataDict),dataDict['Time'])
print('Vbatt =', dataDict['Vbatt'])
print('*********** done ********** \n\r')
////////////////////////////////////////////////////////////////////////////
///////
////// ERROR FROM RUNNING THE JSON LOAD PROGRAM
/////////////////////////////////
////////////////////////////////////////////////////////////////////////////
///////
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART
================================
>>>
Traceback (most recent call last):
File "C:/Python27/myTestRcv00.py", line 17, in <module>
dataDict = json.load(f)
File "C:\Python27\lib\json\__init__.py", line 290, in load
**kw)
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 368, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 85 - line 1 column 421 (char 84 - 420)
>>>
More information about the Tutor
mailing list