python - I want to get the last element and the 3rd to the last element of each line in a .txt file -


this .txt file contains:

ecs10 4

ecs20 b 3

jpn4 c 5

phy9a d 5

here program last element of each line:

f = open("grade_file.txt", "r")  line = f.readlines()  in (line):      print(i[-1:]) 

for reason, when prints each line out, each line has space @ end before gets number (except last line).

so prints out number 5 @ end because last line didn't have space. how can last element , third element calculate gpa?

strip whitespace lines before grabbing last element:

f = open("grade_file.txt", "r") line = f.readlines()  in (line):     = i.strip()     print(i[-1:]) 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -