Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json load problem
#1
So I have the attached code, which works, MOSTLY

The source codes are attached (1 class file, and main file) to preserve spacing, etc.  The main purpose of the code is to work with importing class...

when running the main file ...for example entering FOO (for name) BAR (for key) and MOO (for value) then [y] then FOOBAR0 (for filename)...this works fine.
Exit program
Rerun ...enter FOO (for name, again) BAR (for key) and MOO (for value) then [y] then FOOBAR0 (DON'T CHANGE FILENAME) and it yields an error.  BUt if you go to terminal/command prompt and you will see that ALL data is in FOOBAR0.json  

SO: my question is what makes json.load break when I use it for an already existing file - like the 2nd time I run the program


#userdict0.py

import json
from userdict0cl import D

dname=str(input('select dictionary name: '))
dname= D()

while True:
	keyy=str(input('enter key: '))
	if keyy =='':
		break
	else:
		vall = str(input('enter value: '))
	dname.fill(keyy,vall)
dname.printall()

saveyesno=str(input('want to save dictionary? [y/n] '))
if saveyesno =='n' or saveyesno =='':
	print('ok.')
else:
	txt=str(input('enter file name. '))
	txtfile = txt+'.json'
	print('you\'re file is: ' + txtfile)
	dname.write_items(txtfile)
	print('well done.')
	dname.print_json()
#userdict0cl.py
import json
class D():
	def __init__(self):
		self._items = {}
	def fill(self,a,b):		
		self.a = a
		self.b = b
		self._items[self.a]=self.b
	def printall(self):
		print(self._items)
		return self._items
	def write_items(self,filename):
		self._filename=filename
		with open(self._filename, 'a+') as f:
			json.dump(self._items, f)
			f.close()
	def print_json(self):
		with open(self._filename, 'r') as f:
			fn = json.load(f)
			#fn.seek(0)
			print(fn)
			f.close()

.py   userdict0cl.py (Size: 486 bytes / Downloads: 658)
.py   userdict0.py (Size: 544 bytes / Downloads: 593)
Reply


Messages In This Thread
json load problem - by mepyyeti - Dec-27-2017, 06:10 AM
RE: json load problem - by buran - Dec-27-2017, 07:32 AM
RE: json load problem - by mepyyeti - Dec-27-2017, 07:13 PM
RE: json load problem - by Larz60+ - Dec-27-2017, 08:32 PM
RE: json load problem - by mepyyeti - Dec-27-2017, 09:44 PM
RE: json load problem - by Larz60+ - Dec-28-2017, 02:05 AM
RE: json load problem - by Larz60+ - Dec-28-2017, 02:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  JSON Dump and JSON Load foxholenoob 8 1,085 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  Problem with nested JSON Kalet 7 2,782 Dec-09-2021, 11:13 PM
Last Post: Gribouillis
  Problem With Database Calls and Load Timbo03 1 2,123 Nov-21-2021, 10:48 AM
Last Post: Timbo03
  Problem with Json Array kwekey 2 1,669 Aug-02-2021, 05:11 PM
Last Post: kwekey
  Problem to parse a json enigma619 3 2,375 Dec-04-2020, 08:16 AM
Last Post: enigma619
  Python - help with getting JSON from one DB and load to another DB qIekm 4 3,258 Apr-16-2020, 07:07 AM
Last Post: qIekm
  json problem enigma619 9 3,682 Dec-19-2019, 08:29 AM
Last Post: enigma619
  problem with mapnik in anaconda python 2: from _mapnik import * ImportError: DLL load parsley 0 1,906 Dec-11-2019, 07:50 AM
Last Post: parsley
  Output to a json file problem Netcode 3 3,724 Nov-22-2019, 01:44 AM
Last Post: Skaperen
  Load JSON file data into mongodb using pymongo klllmmm 1 11,860 Jun-28-2019, 12:47 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

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