I am trying to brush up on Python & I have come to a halt with figuring out how to deal with .txt documents. I am challenging myself to make a program that can calculate GPA.
I decided to make my program a little more complicated and instead of a list I want to read a .txt and scrape the relevant information out of it and turn it into a list. I want the list in the format:
(list = [Grades, Credits] list = [97.0, 3.0])
How do I target only the information I want? I have tried a couple things, but each have their own minuet issue, so I will share the code that got me closest to my end point. My code/output & .txt is as follows:
Please let me know if I any other information is needed to make better sense of what I am attempting & Thank you!
Code:
digits = []
n = 0
digit_str = ""
with open("Grades.txt", "r") as g_c_txt:
lines = g_c_txt.readlines()
print(lines)
for line in lines:
for i in line:
if (i.isdecimal() or i == "." or i == "|") == True:
if (n == 0):
i = ""
n += 1
digit_str += i
digits = digit_str.split("|")
print()
print(digits)
Output:
['Government | 97.0% | 3.0 Credits |\n', 'History | 87.0% | 3.0 Credits |\n', 'College Algebra | 76.0% | 4.0 Credits |\n', 'Trignometry | 93.0% | 3.0 Credits |\n', 'Pre-Calculus | 83.0% | 4.0 Credits |\n', 'English 1 | 75.0% | 3.0 Credits |\n', 'Calculus 1 | 56.6% | 4.0 Credits |\n', 'English 2 | 89.4% | 3.0 Credits |']
['97.0', '3.0', '', '87.0', '3.0', '', '76.04.0', '', '93.0', '3.0', '', '83.0', '4.0', '1', '75.0', '3.0', '1', '56.6', '4.0', '2', '89.4', '3.0', '']
.txt:
Government | 97.0% | 3.0 Credits |
History | 87.0% | 3.0 Credits |
College Algebra | 76.0% | 4.0 Credits |
Trignometry | 93.0% | 3.0 Credits |
Pre-Calculus | 83.0% | 4.0 Credits |
English 1 | 75.0% | 3.0 Credits |
Calculus 1 | 56.6% | 4.0 Credits |
English 2 | 89.4% | 3.0 Credits |
If I get the text using txt_text.readlines() and use a for loop to iterate and check if each is a decimal using i.isdecimal() it nearly works except I don't understand how to get rid of multiple extra "|". It also grabs "1, 1, 2" from "English 1, Calculus 1, and English 2". I know I could make this easier by simplifying the .txt, but I won't always be able to choose the format I get my data in and want to learn how to process a bit more complicated documents.
Please Note: the line 3 of 'a.txt' you 've given as
College Algebra | 76.0% - 4.0 Credits |which does not match the format of other lines. So correct the type error toCollege Algebra | 76.0% | 4.0 Credits |.In your question the format of the required output is not clear. So, I'm giving as multidimensional list
In case if you want keys in a list and values in a list in the output list, change the line 22 to
sub = [[x[0], x[3]], [int(float(x[1])), int(float(x[2]))]]