Python String Formatting and Printing
Python has a very easy and nice way of printing text any stream like Stdout/file stream etc. You can easily create some quick nice tabular output and push to file.
print provides an argument where you can specify the stream you want to write to. By default it will write to the StdOut stream.
To read more about python formatting check
lists = [
["Stock", "Count", "Price"],
["AA",100,32.20],
["IBM",50,91.10],
["CAT",150,83.44],
["MSFT",200,51.23],
["IBM",100,70.44]
]
# Template to print out the result
template = '{:>5s} | {:>5d} | {:>5.1f}'
for index, items in enumerate(lists):
if index != 0:
print(template.format(items[0], items[1], items[2]))
# Redirect the printout to a file using print command
with open("Output.txt", "w") as f:
for index, items in enumerate(lists):
if index != 0:
print(template.format(items[0], items[1], items[2]), file=f)
else:
print('{:>5s} | {:>5s} | {:>5s}'.format(lists[0][0], lists[0][1], lists[0][2]), file=f)
One of the cool and powerful function in python is print. If you have a list and sometimes we want to print the list item on each line. Print makes this pretty easy by combining it with destructuring and adding a separator as a new line.
from fnmatch import fnmatch
addresses = [
"5412 W CLARK ST",
"1060 W ADDISON ST",
"4802 N BROADWAY"
]
streets = [address for address in addresses if fnmatch(address, "* ST")]
print(*streets, sep="\n", end="\n")
Some other cool stuff in python are documented in this notebook.
Thanks