I'm having issues trying to properly format a text file to fit the needed criteria for a school project. I've been stuck on it for a while and I'm still very new to coding and wanted to know if anyone has an answer that I can understand and implement, hopefully I can learn from those much more experienced.
I want to convert a text file that can be entered by a user that looks like this within the file:
Lennon 12 3.33
McCartney 57 7
Harrison 11 9.1
Starr 3 4.13
and create it to fit a tabular format like this:
Name Hours Total Pay
Lambert 34 357.00
Osborne 22 137.50
Giacometti 5 503.50
I can create the headers, though it may not be pretty code, but when I print the contents of the test file it usually turns out like this:
Name Hour Total pay
Lennon 12 3.33
McCartney 57 7
Harrison 11 9.1
Starr 3 4.13
And I don't understand how to properly format it to look like a proper table that's right justified and properly in line with the actual headers, I'm not sure how to really tackle it or where to even start as I haven't made any real ground on this.
I've gutted my code and broke it down into just the skeleton after trying with things like file_open.read().rstrip("\n)
and .format
making a mess of the indexes and sometimes somehow ending up with only single letters to appear:
file_name = input("Enter the file name: ")print("Name" + " " * 12 + "Hour" + " " * 6 + "Total pay")
with open(file_name, 'r') as f:for line in f:print(line, end='')
I know it looks simple, and because it is. Our instructor wanted us to work with the "open" command and try and stay away from things that could make it less readable but still as compact as possible. This includes the importing of third party tools which shot down chances to use things like beautifultable like a few other friends have offered as an easier way out.
I had a classmate say to read the lines that turns it into a list and adjust it from there with some formatting, and another classmate said I could probably format it without listing it; although I found that the newline character "\n" appears at the end of each list index if turning it into a list
ex: ['Lennon 12 3.33\n', 'McCartney 57 7\n', 'Harrison 11 9.1\n', 'Starr 3 4.13']
Though what I don't understand is how to format the things that are within the list so that the name can be separated from each of the number variables and in line with the header as I don't have much experience with for loops that many say can be an easy fix within my class if I have that down pat.
I'm not exactly looking for straight coded answers, but rather a point in the right direction or where to read up on how to manipulate listed content