Posts: 5
Threads: 2
Joined: Aug 2018
Aug-27-2018, 07:38 PM
(This post was last modified: Aug-27-2018, 07:43 PM by buran.)
Hello,
Trying to append values to key value pair in dictionary, meaning the value becomes a list.
As context, script runs a system call, writes variable to json file, wakes in an hour and repeats, always appending to key many values. Trying to produce samples for a standard deviation
Here is code that mostly works:
oldJsonFileDict = {}
newestJsonDict = {}
with open('/tmp/ntpOutput.json') as fr:
oldJsonFileDict = json.load(fr)
fr.close()
newestJsonDict['offset'] = []
newestJsonDict['offset'].append(oldJsonFileDict['offset'])
newestJsonDict['offset'].append(varOne)
with open('/tmp/ntpOutput.json', 'w') as fw:
fw.write(json.dumps(newestJsonDict))
fw.close() MY QUESTION IS: why would my output have multiple ['s and ]'s LIKE SO: {"offset": [[["32.851", "32.835"], "32.819"], "32.803"]}
Posts: 8,160
Threads: 160
Joined: Sep 2016
this code is not full, but that is what you do on lines 8-10, e.g. on line 9 you append to the newestJsonDict['offset'], which is already a list something that most probably is already a list. I guess oldJsonFileDict['offset'] value is actually [["32.851", "32.835"], "32.819"]
you can print newestJsonDict['offset'] after each line 8-10 to see how it changes.
Also note that when using with context manager it will close the file for you. You don't need lines 6 and 14
Posts: 4,220
Threads: 97
Joined: Sep 2016
Two questions: What is varOne? and What do you get when you print oldJsonFileDict['offset']?
Posts: 5
Threads: 2
Joined: Aug 2018
Aug-28-2018, 01:06 PM
(This post was last modified: Aug-28-2018, 01:36 PM by hokie1999.)
@ ichabod801 -- varOne is a variable representing ntp drift that is produced once a minute because the script runs once a minute. varOne is the variable that is always appended to the list.
when i add print statement, as you asked, it produces this: [[["32.851", "32.835"], "32.819"], "32.803"]
Looks like a list inside of a list inside of a list...
If i alter code like so:
newestJsonDict['offset'] = []
newestJsonDict['offset']=oldJsonFileDict['offset'] <<<<<<< changed "append" function to =
newestJsonDict['offset'].append(varOne)
get this
File "/root/fixNtp.py", line 42, in processStats <<<< main function that is calling all this
newestJsonDict['offset'].append(varOne)
AttributeError: 'unicode' object has no attribute 'append'
My code is unicode, the default format for json
Posts: 8,160
Threads: 160
Joined: Sep 2016
what I meant is
oldJsonFileDict = {}
newestJsonDict = {}
with open('/tmp/ntpOutput.json') as fr:
oldJsonFileDict = json.load(fr)
print('Old json offset', oldJsonFileDict['offset'])
print('varOnet', varOne)
newestJsonDict['offset'] = []
newestJsonDict['offset'].append(oldJsonFileDict['offset'])
print('New json dict offset', newestJsonDict['offset'])
newestJsonDict['offset'].append(varOne)
print('New json dict offset', newestJsonDict['offset'])
with open('/tmp/ntpOutput.json', 'w') as fw:
fw.write(json.dumps(newestJsonDict)) let's see what you work with...
and show what you want to get at the end
Posts: 5
Threads: 2
Joined: Aug 2018
Aug-28-2018, 06:10 PM
(This post was last modified: Aug-28-2018, 06:11 PM by hokie1999.)
output....
('varOne', '-182.061')
('New json dict offset', [[[[[[u'3.193', u'3.193'], u'3.191'], u'3.189'], u'3.188'], u'3.186']])
('New json dict offset', [[[[[[u'3.193', u'3.193'], u'3.191'], u'3.189'], u'3.188'], u'3.186'], '-182.061'])
...from suggested changes....
def processStats():
firstDict = {}
varOne = getStats() <<<<<<< gets ntp drift, a float
if not os.path.exists("/tmp/ntpOutput.json"): <<<<< first and only time script runs
firstDict['offset'] = varOne
with open('/tmp/ntpOutput.json', 'w') as fw:
#json.dump(firstDict, fw)
fw.write(json.dumps(firstDict))
else: <<<<<<<<<<<<<< all other times script runs
time.sleep(1) <<<< for testing purposes only
oldJsonFileDict = {}
newestJsonDict = {}
with open('/tmp/ntpOutput.json') as fr:
oldJsonFileDict = json.load(fr)
print('Old json offset', oldJsonFileDict['offset'])
print('varOne', varOne)
newestJsonDict['offset'] = []
newestJsonDict['offset'].append(oldJsonFileDict['offset'])
print('New json dict offset', newestJsonDict['offset'])
newestJsonDict['offset'].append(varOne)
print('New json dict offset', newestJsonDict['offset'])
with open('/tmp/ntpOutput.json', 'w') as fw:
fw.write(json.dumps(newestJsonDict))
Posts: 8,160
Threads: 160
Joined: Sep 2016
Aug-28-2018, 06:36 PM
(This post was last modified: Aug-28-2018, 06:37 PM by buran.)
Repost with proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Also, post the result of printing print('Old json offset', oldJsonFileDict['offset'])
Actualy, note that every time you run the code you change your ntpOutput.json. And next time you run the code don't work with the original file, but the new amended one, with nested lists. Next time it will have even more nested lists.
|