में ले कर रीयर्सन के माध्यम से अजगर में एक पेड़ बनाएं, मैं पाइथन के लिए नया हूं और मुझे टेक्स्ट फ़ाइल
से इनपुट में लेने के बाद अजगर में एक पेड़ बनाना है, मेरे पास टेक्स्ट फ़ाइल में निम्न डेटा है। मैं 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": []
}
]
}
}
मैं नीचे दिए गए कोड लिखा के साथ अजगर में एक पेड़ का निर्माण करने के लिए है, लेकिन यह वाक्यविन्यास त्रुटियों अगर किसी भी एक मिल सकता है जो इम असमर्थ सही करने के लिए है उन्हें
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
त्रुटि मैं दिखाई दे रही है एक डिकोडिंग JSON से संबंधित त्रुटि है (ऐसा लगता है कि आप अंतिम पंक्ति के बाद दूसरे स्थान पर एक बंद करने वर्ग कोष्ठक याद कर रहे हैं) –
और इसकी बिल्डनोड विधि –