Python Forum
Help! Formatting and Writing to a File
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help! Formatting and Writing to a File
#1
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
Reply
#2
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.
Reply
#3
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting a date time string read from a csv file DosAtPython 5 1,281 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,377 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,005 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,489 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Formatting to output file Mark17 2 1,314 Jan-13-2022, 09:06 PM
Last Post: BashBedlam
  Importing text file into excel spreadsheet with formatting david_dsmn 1 3,615 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
  Writing to file ends incorrectly project_science 4 2,690 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Logging to a file - Formatting Maxximiliann 0 1,442 Dec-18-2020, 02:06 AM
Last Post: Maxximiliann
  Writing unit test results into a text file ateestructural 3 4,747 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,416 Nov-13-2020, 10:10 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020