Use python to pull info from a .txt file and sum numbers by customer and by product

  python

Sorry if this is relatively simple but I am new to python. I need to pull the product amount from this text file using python and sum the values by customer name and sum the values by product code.
Desired outputs should look like:

Sum by customer name should print out:
Lynn 3700
Ken 1350
Dave 1000

Sum by product code should print out:
MAF 2750
HAR 1950
RSF 1350

Image description
This is the transaction.txt file i need to pull the data from

f2 = open("transactions.txt", "r")          #open and read transactions.txt file
headers2 = f2.readline()
data2 = f2.readlines()
print(headers2)
reportList = {}
for line in data2:
    line_split = line.split()
    reportList[line_split[0]] = [line_split[1],line_split[2]]#,line_split[3]]


grouping_choice = input("How would you like to group the sales - [P]: by product or [C]: by customer?").lower()
if grouping_choice == "p":                       #product grouping
    print(f'Product tt Total Sales')
   
    menu()                                     

elif grouping_choice == "c":                     #customer grouping
    print(f'Customer tt Total Sales')

    menu()
else:
    print("Error, you selected an invalid choice!")       #invalid selection, sends back to report menu
    report()

Source: Python Questions

LEAVE A COMMENT