2017-12-17 8 views
0

Ich habe ein Wörterbuch der Wörterbücher in Python, die wie folgt aussieht:Nested Wörterbuch JSON Nested Wörterbuch in Python

{  
    "Europe": { 
     "France": (10,5), 
     "Germany": (15,5), 
     "Italy": (5,15), 
     }, 
"North-America": { 
     "USA": (20,0), 
     "CANADA": (12,4), 
     "MEXICO": (14,8), 
     }, 
} 

Ich möchte das Wörterbuch in einer JSON-Datei speichern, die Daten zu bekommen, wenn ich es brauche. ich das Geschäft wie folgt aus:

with open(filename, 'a') as jsonfile: 
    json.dump(dictionary, jsonfile) 

Das Problem kommt jetzt. Wenn ich versuche, das gespeicherte JSON-Wörterbuch zu lesen, erhalte ich den gleichen Fehler: Python json.loads shows ValueError: Extra data

Die Antwort in diesem Beitrag ist nur, um die verschiedenen dicts in einer Liste zu speichern und alle zu speichern. Aber ich verstehe nicht, wie man es macht, wenn sie verschachtelt sind und sie dynamisch erstellt werden.

So wie ich das json lesen, ist dies:

jsonFile = open(filename) 
data = json.loads(jsonFile) 
jsonFile.close() 
return data 

In Lebenslauf. Ich muss das Wörterbuch aus der JSON-Datei in ein Wörterbuch in Python laden. Wie kann ich das erreichen?

+2

Warum hängen Sie an Ihre Datei an? Verwenden Sie den 'w'-Modus, JSON ist kein streambares Format. – zwer

+0

Mein schlechtes. Ich weiß nicht, warum ich schrieb 'a' Modus, wenn tatsächlich ist 'w' Modus – CodePathLvl

+0

die Lade-Methode ist wie folgt: jsonFile = öffnen (Dateiname) data = json.loads (jsonFile) jsonFile.close() geben Sie Daten – CodePathLvl

Antwort

1

Dies ist, wie ich in eine JSON-Datei schreiben und daraus lesen:

import json 
from pprint import pprint 

dictionary = {"Europe": 
      {"France": (10,5), 
       "Germany": (15,5), 
       "Italy": (5,15)}, 

      "North-America": { 
       "USA": (20,0), 
       "CANADA": (12,4), 
       "MEXICO": (14,8)} 
      } 

with open("test.json", 'w') as test: 
    json.dump(dictionary, test) 

# Data written to test.json 
with open("test.json") as test: 
    dictionary = json.load(test) 

pprint(dictionary) 

{'Europe': {'France': [10, 5], 'Germany': [15, 5], 'Italy': [5, 15]}, 
'North-America': {'CANADA': [12, 4], 'MEXICO': [14, 8], 'USA': [20, 0]}} 
>>> 

# Accessing dictionary["Europe"] 
print(dictionary["Europe"]) 

{'France': [10, 5], 'Germany': [15, 5], 'Italy': [5, 15]} 
>>> 

# Accessing items in dictionary["North-America"] 
print(dictionary["North-America"].items()) 

dict_items([('USA', [20, 0]), ('CANADA', [12, 4]), ('MEXICO', [14, 8])]) 
>>> 

bearbeiten:

: Sie können es wie ein normales Wörterbuch

# Convert your input dictionary to a string using json.dumps() 
data = json.dumps(dictionary) 

# Write the string to a file 
with open("test.json", 'w') as test: 
    test.write(data) 

# Read it back 
with open("test.json") as test: 
    data = test.read() 

# decoding the JSON to dictionary 
d = json.loads(data) 

print(type(d)) 

<class 'dict'> 
>>> 

Jetzt nutzen könnten

>>> d["Europe"] 
{'France': [10, 5], 'Germany': [15, 5], 'Italy': [5, 15]} 
>>> d["North-America"].items() 
dict_items([('USA', [20, 0]), ('CANADA', [12, 4]), ('MEXICO', [14, 8])]) 
>>> 
+0

Ich verstehe. Ich werde es versuchen und sagen, was ich habe. – CodePathLvl

+0

Vielen Dank. Das war das Problem, ich schrieb und las, ohne den Kodierungs-/Dekodierungsprozess zu machen. – CodePathLvl