-1

with a given text of:

Daily, John
10.0   9.5    8.0  10.0
 9.5  10.0    9.0
85.0  92.0   81.0

I am given this information on an imported .txt file and need to open and read the file and then write and save a new text file, which i know how to do, where I am stuck on is how i would manipulate each line of the read text separately, as the first line (the name) needs to be saved, but second line needs to be calculated into a quiz average amount and then the 3rd line a homework average and the fourth line an exam average and then all of them weighted separately and saved next to the saved name of the student.

note: after the fourth line starts a new student with the same information.

edit: i guess the simplest way to do this would be to have a for loop that read 4 lines of text at a time and worked each of those lines seperately then read another four lines. Is there any way to read in sets of lines instead of one line at a time?

MilkSteak
  • 9
  • 1
  • 2
    Possible duplicate of [Get a list of numbers as input from the user](https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user) – Ken Y-N Apr 15 '18 at 23:40
  • Start with [this](https://stackoverflow.com/questions/5832856/how-to-read-file-n-lines-at-a-time-in-python). Once you have read 4 lines you can individually manipulate them. So first split the values, then do the calculations on the 2nd, 3rd and 4th lines. After that you can collate the values to calculate the grades. – Paul Rooney Apr 16 '18 at 00:02

1 Answers1

0

Use str.splitlines and str.split.

Example:

>>> txt = '''10.0   9.5    8.0  10.0
 9.5  10.0    9.0
85.0  92.0   81.0
'''
>>> for line in txt.splitlines():
...     print(line.split())
['10.0', '9.5', '8.0', '10.0']
['9.5', '10.0', '9.0']
['85.0', '92.0', '81.0']

or get only one column:

>>> for line in txt.splitlines():
...     print(line.split()[0])
...
10.0
9.5
85.0
fferri
  • 18,285
  • 5
  • 46
  • 95
  • But, how would i do it in a manner to work with each line individually? for example i will be outputting information from the first line (the name), then computing a quiz average of the second line and out putting that, and so on and so forth and then on the fifth line it will be a new name of a new student. So how would i go about seperating each line individually so it could receive it's own for-loop – MilkSteak Apr 15 '18 at 23:41