Skip to content

Tag: tree

Python Element Tree Writing to New File

Hi so I’ve been struggling with this and can’t quite figure out why I’m getting errors. Trying to export just some basic XML into a new file, keeps giving me a TypeError. Below is a small sample of the code Answer The ElementTree.write method defaults to us-ascii encoding and as such expects…

Python – Tree without classes

How can I implement a tree without using classes in python? I can only use lists, dictionaries and queue. Obviously without the library bintree. Answer I usually use defaultdict: Usage: bonus: Maria Callas singing O Mio Babbino Caro

Python file parsing: Build tree from text file

I have an indented text file that will be used to build a tree. Each line represents a node, and indents represent depth as well as node the current node is a child of. For example, a file might look like ROOT Node1 Node2 Node3 Node4 Node5 Node6 Which indicates that ROOT contains three children: 1, 5, and 6, …