Python Forum
lots of ['s and ]'s in key value list append
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lots of ['s and ]'s in key value list append
#1
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"]}

Dodgy
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Two questions: What is varOne? and What do you get when you print oldJsonFileDict['offset']?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
@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
Reply
#5
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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))
Reply
#7
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 705 Mar-14-2024, 06:26 PM
Last Post: flash77
Question How to append integers from file to list? Milan 8 1,595 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  read a text file, find all integers, append to list oldtrafford 12 3,903 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Using .append() with list vs dataframe Mark17 7 11,298 Jun-12-2022, 06:54 PM
Last Post: Mark17
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,414 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  How to append to list a function output? rama27 5 6,925 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  Append list into list within a for loop rama27 2 2,477 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  Append only adding the same list again and again viraj1123 4 2,127 Jun-17-2020, 07:26 AM
Last Post: viraj1123
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,676 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  Problem with append list in loop michaelko03 0 1,727 Feb-16-2020, 07:04 PM
Last Post: michaelko03

Forum Jump:

User Panel Messages

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