Skip to content Skip to sidebar Skip to footer

Python - Write Matrix To Txt File, Keep Formatting

I am struggeling in writing the output of my code to a txt file while keeping the format. Here is the code: import os # Compute matrix titles = ['Filename', 'Date'] matrix = [titl

Solution 1:

If you're really only trying to write the string you're currently printing to a file, then it's not much more than this:

out = ""

for row in matrix:
    for column, data in enumerate(row):
        out += formats[column].format(data)
    out += "\n"

with open("out.txt","wt") as file:
    file.write(out)

Post a Comment for "Python - Write Matrix To Txt File, Keep Formatting"