parsing tree from excel sheet
alb
al.basili at gmail.com
Wed Jan 28 09:27:38 EST 2015
Hi Peter,
Peter Otten <__peter__ at web.de> wrote:
[]
> You can save the excel sheet as csv so that you an use the csv module which
> may be easier to use than xlrd. The rest should be doable by hand. Here's
> what I hacked together:
>
> $ cat parse_column_tree.py
> import csv
>
> def column_index(row):
> for result, cell in enumerate(row, 0):
> if cell:
> return result
> raise ValueError
>
>
> class Node:
> def __init__(self, name, level):
> self.name = name
> self.level = level
> self.children = []
>
> def append(self, child):
> self.children.append(child)
>
> def __str__(self):
> return "\%s{%s}" % (self.level, self.name)
>
> def show(self):
> yield [self.name]
> for i, child in enumerate(self.children):
> lastchild = i == len(self.children)-1
> first = True
> for c in child.show():
> if first:
> yield ["\---> " if lastchild else "+---> "] + c
> first = False
> else:
> yield [" " if lastchild else "| "] + c
> def show2(self):
> yield str(self)
> for child in self.children:
> yield from child.show2()
>
> def show(root):
> for row in root.show():
> print("".join(row))
>
> def show2(root):
> for line in root.show2():
> print(line)
>
> def read_tree(rows, levelnames):
> root = Node("#ROOT", "#ROOT")
> old_level = 0
> stack = [root]
> for i, row in enumerate(rows, 1):
>
> new_level = column_index(row)
> node = Node(row[new_level], levelnames[new_level])
>
> if new_level == old_level:
> stack[-1].append(node)
> elif new_level > old_level:
> if new_level - old_level != 1:
> raise ValueError
>
> stack.append(stack[-1].children[-1])
> stack[-1].append(node)
> old_level = new_level
> else:
> while new_level < old_level:
> stack.pop(-1)
> old_level -= 1
> stack[-1].append(node)
> return root
>
> def main():
> import argparse
> parser = argparse.ArgumentParser()
> parser.add_argument("infile")
> parser.add_argument("--latex", action="store_true")
>
> args = parser.parse_args()
>
> with open(args.infile) as f:
> rows = csv.reader(f)
> levelnames = next(rows) # skip header
> tree = read_tree(rows, levelnames)
>
> show_tree = show2 if args.latex else show
> for node in tree.children:
> show_tree(node)
> print("")
>
> if __name__ == "__main__":
> main()
> $ cat data.csv
> subsystem,chapter,section,subsection,subsubsec,
> A,,,,,
> ,func0,,,,
> ,,interface,,,
> ,,latency,,,
> ,,priority,,,
> ,func1,,,,
> ,,interface,,,
> ,,latency,,,
> ,,priority,,,
> ,,depend,,,
> ,,,variables,,
> ,,,,static,
> ,,,,global,
> ,,,functions,,
> ,,,,internal,
> ,,,,external,
> $ python3 parse_column_tree.py data.csv
> A
> +---> func0
> | +---> interface
> | +---> latency
> | \---> priority
> \---> func1
> +---> interface
> +---> latency
> +---> priority
> \---> depend
> +---> variables
> | +---> static
> | \---> global
> \---> functions
> +---> internal
> \---> external
>
> $ python3 parse_column_tree.py data.csv --latex
> \subsystem{A}
> \chapter{func0}
> \section{interface}
> \section{latency}
> \section{priority}
> \chapter{func1}
> \section{interface}
> \section{latency}
> \section{priority}
> \section{depend}
> \subsection{variables}
> \subsubsec{static}
> \subsubsec{global}
> \subsection{functions}
> \subsubsec{internal}
> \subsubsec{external}
WOW! I didn't really want someone else to write what I needed but thanks
a lot! That's a lot of food to digest in a single byte, so I'll first
play a bit with it (hopefully understanding what is doing) and then come
back with comments.
I really appreciated your time and effort.
Al
More information about the Python-list
mailing list