![]() |
Help! Formatting and Writing to a File - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help! Formatting and Writing to a File (/thread-26045.html) |
Help! Formatting and Writing to a File - bwdu - Apr-19-2020 Hi, everyone! This is my code below; file = open("filtereddata.txt","w") b = [list(range(x,x+10)) for x in range(0, 100, 10)] #headings i = "|0|" i1 = "|1|" i2 = "|2|" i3 = "|3|" i4 = "|4|" i5 = "|5|" i6 = "|6|" i7 = "|7|" i8 = "|8|" i9 = "|9|" text1 = ("%-4s %-4s %-4s %-4s %-4s %-4s %-4s %-4s %-4s %-4s\n" % (i, i1, i2, i3, i4, i5, i6, i7, i8, i9 )) text2 = ("%-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d\n" % (b[0][0], b[0][1], b[0][2], b[0][3], b[0][4], b[0][5], b[0][6], b[0][7], b[0][8], b[0][9])) text3 = ("%-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d\n" % (b[1][0], b[1][1],b[1][2] , b[1][3], b[1][4], b[1][5], b[1][6], b[1][7], b[1][8], b[1][9])) text4 = ("%-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d\n" % (b[2][0], b[2][1], b[2][2], b[2][3], b[2][4], b[2][5], b[2][6], b[2][7], b[2][8], b[2][9])) text5 = ("%-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d\n" % (b[3][0], b[3][1], b[3][2], b[3][3], b[3][4], b[3][5], b[3][6], b[3][7], b[3][8], b[3][9])) text6 = ("%-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d\n" % (b[4][0], b[4][1], b[4][2], b[4][3], b[4][4], b[4][5], b[4][6], b[4][7], b[4][8], b[4][9])) text7 = ("%-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d\n" % (b[5][0], b[5][1], b[5][2], b[5][3], b[5][4], b[5][5], b[5][6], b[5][7], b[5][8], b[5][9])) text8 = ("%-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d\n" % (b[6][0], b[6][1], b[6][2], b[6][3], b[6][4], b[6][5], b[6][6], b[6][7], b[6][8], b[6][9])) text9 = ("%-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d\n" % (b[7][0], b[7][1], b[7][2], b[7][3], b[7][4], b[7][5], b[7][6], b[7][7], b[7][8], b[7][9])) text10 = ("%-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d\n" % (b[8][0], b[8][1], b[8][2], b[8][3], b[8][4], b[8][5], b[8][6], b[8][7], b[8][8], b[8][9])) text11 = ("%-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d %-4d\n" % (b[9][0], b[9][1], b[9][2], b[9][3], b[9][4], b[9][5], b[9][6], b[9][7], b[9][8], b[9][9])) file.write(text1) file.write(text2) file.write(text3) file.write(text4) file.write(text5) file.write(text6) file.write(text7) file.write(text8) file.write(text9) file.write(text10) file.write(text11)The code writes this table to the file named filtereddata; |0| |1| |2| |3| |4| |5| |6| |7| |8| |9| 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 I am wondering if the formatting part(which I used for aligning colums(i,i2...)) could be shorter. I will be appriciated from any idea RE: Help! Formatting and Writing to a File - bowlofred - Apr-19-2020 You could store the format in a variable and then just use the variable. format_string = "%-4s %-4s %-4s %-4s %-4s %-4s %-4s %-4s %-4s %-4s\n" text1 = (format_string % (i, i1, i2, i3, i4, i5, i6, i7, i8, i9 ))Instead of doing your own explicit formatting, you might want to pull down a table formatting package like Tabulate or Pretty Table. You set up the formatting once and let it handle everything. RE: Help! Formatting and Writing to a File - deanhystad - Apr-19-2020 file = open("filtereddata.txt","w+") b = [list(range(x,x+10)) for x in range(0, 100, 10)] headers = ["|0|","|1|","|2|","|3|","|4|","|5|","|6|","|7|","|8|","|9|"] for header in headers: file.write(f'{header: >4s} ') file.write('\n') for row in b: for col in row: file.write(f'{col:4d} ') file.write('\n') print(row) file.close() |