我是python的新手,我必须在从文本文件中获取输入后在python中构建一个树,
我在文本文件中包含以下数据.我必须使用Json在python中使用以下数据构建一个树
{
"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": []
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我写了下面的代码,但它有语法错误,我无法纠正,如果任何人都可以找到它们
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
Run Code Online (Sandbox Code Playgroud)
import json
class Node(object):
def __init__(self, component=None, status=None, level=0):
self.component = component
self.status = status
self.level = level
self.children = []
def __repr__(self):
return '\n{indent}Node({component},{status},{children})'.format(
indent = self.level*'\t',
component = self.component,
status = self.status,
children = repr(self.children))
def add_child(self, child):
self.children.append(child)
def tree_builder(obj, level=0):
node = Node(component=obj['component'], status=obj['status'], level=level)
for child in obj.get('children',[]):
node.add_child(tree_builder(child, level=level+1))
return node
def load_json(filename):
with open(filename) as f:
return json.load(f)
obj = load_json('test.json')
tree = tree_builder(obj)
print tree
Run Code Online (Sandbox Code Playgroud)
输出:
Node(A,0,[
Node(AA,0,[
Node(AAA,0,[]),
Node(AAB,0,[])]),
Node(AB,0,[
Node(ABA,0,[]),
Node(ABB,0,[])])])
Run Code Online (Sandbox Code Playgroud)
好的,我能够修复您代码中的错误,现在它看起来像这样:
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
#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',node1.component,'','status',node1.status
for children in ob['children']:
node1.children.append(self.buildnode(children))
return node1
#Building Json object from text file
class start:
import json
f=open("json_file.txt")
data=json.load(f)
builder = implementation()
builder.buildnode(data)
Run Code Online (Sandbox Code Playgroud)
这会产生以下输出:
component A status 0
component AA status 0
component AAA status 0
component AAB status 0
component AB status 0
component ABA status 0
component ABB status 0
Run Code Online (Sandbox Code Playgroud)
以下是对所需内容的一些解释:
首先,您在定义 buildnode() 之前调用它,它是一个类函数,因此在调用它之前您需要一个该类的实例。接下来,当您调用字典中的值时,您需要通过 来访问它们dictionary['key']。唯一的另一件大事是附加到数组的方法是调用.append()而不是.add()。
| 归档时间: |
|
| 查看次数: |
4062 次 |
| 最近记录: |