Jestem nowy w python i muszę zbudować drzewo w pytonie po pobraniu z pliku tekstowego
Mam poniższe dane w pliku tekstowym. Muszę zbudować drzewo w Pythonie z poniższym danych za pomocą JSONZbuduj drzewo w pythonie przez rekurencję przez wzięcie obiektu json
{
"component": "A",
"status": 0,
"children": [
{
"component": "AA",
"status": 0,
"children": [
{
"component": "AAA",
"status": 0,
"children": []
},
{
"component": "AAB",
"status": 0,
"children": []
}
]
},
{
"component": "AB",
"status": 0,
"children": [
{
"component": "ABA",
"status": 0,
"children": []
},
{
"component": "ABB",
"status": 0,
"children": []
}
]
}
}
napisałem poniższy kod, ale zawiera błędy składniowe, które Im stanie poprawne, jeśli ktoś może je znaleźć
class node:
#Construction of Node with component,status and children
def _init_(self,component=None,status=None,children=None):
self.component = component
self.status = status
if children is None:
self.children = []
else:
self.children = children
#Building Json object from text file
class start:
import json
f=open("json_file.txt")
data=json.load(f)
buildnode(data)
#Construction of tree through recursion
class implementation:
def buildnode(self,ob):
node1= node()
node1.component=ob.component
node1.status=ob.status
node1.children=[]
print 'component',component,'','status',status
for children in ob:
node1.children.add(buildnode(children[i]))
return node1
Błąd widzę jest błąd związany z dekodowania JSON (wygląda na to, że brakuje zamykający nawias kwadratowy na przedostatniej linii) –
A użycie klasy bez wywoływania jej wystąpienia w celu wywołania jej metody buildnode. –