Created
March 7, 2012 13:56
-
-
Save nikoheikkila/1993291 to your computer and use it in GitHub Desktop.
Python: Recursive printer for lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################# | |
# # | |
# PRINTR.PY # | |
# # | |
# Advanced printing for python terminal or file handling # | |
# Homepage: http://pypi.python.org/pypi/printr # | |
# # | |
################################################################# | |
import sys | |
def print_r(the_list, indent=False, level=0, fh=sys.stdout): | |
''' | |
@param the_list The list to iterate over | |
@param indent Option to indent output | |
@param level Number of levels to iterate | |
@param fh Output destination, stdout (screen) by default | |
''' | |
for each_item in the_list: | |
if isinstance(each_item, list): | |
print_r(each_item, indent, level+1, fh) | |
else: | |
if indent: | |
for tab_stop in range(level): | |
print("\t"*level, end='', file=fh) | |
print(each_item, file=fh) | |
# End of file printr.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment