In Python, I'm attempting (very poorly) to read a .txt file, find the last occurrence of a string referencing a particular customer, and read a few lines below that to gain their current points balance.
A snapshot of the .txt file is:
Customer ID:123
Total sale amount:2345.45
Points from sale:23
Points until next bonus: 77
I can search for (and find) the specific Customer ID, but can't figure out how to search for the last occurrence of this ID only, or how to return the 'Points until next bonus' value... I apologise if this is a basic question, but any help would be greatly appreciated!
My code so far...
def reward_points():
#current points total
rewards = open('sales.txt', 'r')
line = rewards.readlines()
search = (str('Customer ID:') + str(Cust_ID))
print(search) #Customer ID:123
while line != ' ':
if line.startswith(search):
find('Points until next bonus:')
current_point_total = line[50:52]
cust_record = rewards.readlines()
print(current_point_total)
rewards.close()
reward_points()