I want to know how to find the total sum of number from a column in a text file in python.
My file:
123 Hammer 20 36
124 Knife 10 10
125 Rod 90 20
I want to add the 4th column in the text file. That is the column that starts with 36. The function should return the sum of the column that is 66.
I don't have any code yet as I am still stuck in this problem and thinking of a way to solve it.
I am having error in some places i think it is due to my code. I need assistance in it.
# Creating class Hardware to store the linked list data in to the class, Properties like get and set
class Hardware:
def __init__(self,barcode,description,price,quantity): # initating data
self.barcode=barcode
self.description=description
self.price=price
self.quantity=quantity
self.next=None
def getData(self):#function to get data
return self.barcode,self.description,self.price,self.quantity
def getNext(self):#function to get the next data
return self.next
def setData(self,newBarcode,newDescription,newPrice,newQuantity):#function to set the data
self.barcode=newBarcode
self.description=newDescription
self.price=newPrice
self.quantity=newQuantity
def setNext(self,newNext):#function to set the next data
self.next=newNext
# LinkedList class for manipulation of data that is add, display and update
class LinkedListHardware:
def __init__(self): # initating data
self.head=None
def isEmpty(self): #checks if the data is empty
return self.head==None
def ReadFile(self): # Reads a text file called Hardware
Hardwarefile=open('Hardware.txt','r')
return Hardwarefile.read()
def add(self,itemBarcode,itemDescription,itemPrice,itemQuantity): #Adds data to the linked list and also writes to the file
temp=Hardware(itemBarcode,itemDescription,itemPrice,itemQuantity)
temp.setNext(self.head)
self.head=temp
HardwareItems=(itemBarcode,itemDescription,itemPrice,itemQuantity)
Hardwarefile=open('Hardware.txt','a+')
Hardwarefile.write('\n')
for items in HardwareItems:
Hardwarefile.write(str(items)+'\t')
return "Added Successfully"
Hardwarefile.close()
def display(self,itemBarcode): # Displays a line based on the Barcode the user enters
current=self.head
with open('Hardware.txt','r') as f:
found=False
for line in f:
lines=line.split()
if itemBarcode in lines:
found=True
return line
if not found:
return "No such Barcode"
f.close()
def update(self,itemBarcode,itemDescription,itemPrice,itemQuantity): #update data of hardware item
current=self.head
with open('Hardware.txt','r+') as f:
found=False
for lines in f:
line=lines.split()
if itemBarcode not in line:
array=[]
array.append(line)
st=[x[0]+'\t'+x[1]+'\t'+x[2]+'\t'+x[3] for x in array]
st=''.join(st)
with open('Hardwareupdate.txt','a+') as outfile:
outfile.write('\n'+str(st))
found=True
HardwareItems=(itemBarcode,itemDescription,itemPrice,itemQuantity)
Hardwarefile=open('Hardwareupdate.txt','a+')
Hardwarefile.write('\n')
for items in HardwareItems:
Hardwarefile.write(str(items)+'\t')
return "Added Successfully"
if not found:
return "No such Transaction"
# Creating class Transaction to store the linked list data in to the class, Properties like get and set
class Transaction:
def __init__(self,invoiceNumber,Barcode,totalPrice,Quantity): # initating data
self.invoiceNumber=invoiceNumber
self.Barcode=Barcode
self.totalPrice=totalPrice
self.Quantity=Quantity
self.next=None
def getData(self): #function to get data
return self.invoiceNumber,self.Barcode,self.totalPrice,self.Quantity
def getNext(self): #function to get the next data
return self.next
def setData(self,newinvoiceNumber,newBarcode,newtotalPrice,newQuantity): #function to set the data
self.invoiceNumber=newinvoiceNumber
self.Barcode=newBarcode
self.totalPrice=newtotalPrice
self.Quantity=newQuantity
def setNext(self,newNext): #function to set the next data
self.next=newNext
# LinkedList class for manipulation of data that is add, search, update and delete
class LinkedListTransaction:
# initating data
def __init__(self):
self.head=None
#checks if the data is empty
def isEmpty(self):
return self.head==None
#Adds data to the linked list and also writes to the file
def add(self,iteminvoiceNumber,itemBarcode,itemQuantity,itemtotalPrice):
temp=Transaction(iteminvoiceNumber,itemBarcode,itemQuantity,itemtotalPrice)
temp.setNext(self.head)
self.head=temp
TransactionItems=(iteminvoiceNumber,itemBarcode,itemQuantity,itemtotalPrice)
Transactionfile=open('Transaction.txt','a+')
Transactionfile.write('\n')
for items in TransactionItems:
if iteminvoiceNumber != ' ':
Transactionfile.write(str(items)+'\t')
return "Added Successfully"
# Displays the record of data by receving the item invoice number
def display(self,iteminvoiceNumber):
current=self.head
with open('Transaction.txt','r') as f:
found=False
for line in f:
lines=line.split()
if iteminvoiceNumber in lines:
found=True
return line
if not found:
return "No such Transaction"
# Removes sepcific data from the file (rewrites data that does not contain the data which user entered and writes to another file)
def remove(self,iteminvoiceNumber):
current=self.head
with open('Transaction.txt','r') as f:
found=False
for lines in f:
line=lines.split()
if iteminvoiceNumber not in line:
array=[]
array.append(line)
st=[x[0]+'\t'+x[1]+'\t'+x[2]+'\t'+x[3] for x in array]
st=''.join(st)
with open('Transactionlatest.txt','a+') as outfile:
outfile.write('\n'+str(st))
found=True
if not found:
return "No such Transaction"
# Updates Data from the text file. Copies data to new file and then update whatever user enters
def update(self,iteminvoiceNumber,itemBarcode,itemQuantity,itemtotalPrice):
current=self.head
with open('Transaction.txt','r+') as f:
found=False
for lines in f:
line=lines.split()
if iteminvoiceNumber not in line:
array=[]
array.append(line)
st=[x[0]+'\t'+x[1]+'\t'+x[2]+'\t'+x[3] for x in array]
st=''.join(st)
with open('Transactionupdate.txt','a+') as outfile:
outfile.write('\n'+str(st))
found=True
TransactionItems=(iteminvoiceNumber,itemBarcode,itemQuantity,itemtotalPrice)
Transactionfile=open('Transactionupdate.txt','a+')
Transactionfile.write('\n')
for items in TransactionItems:
Transactionfile.write(str(items)+'\t')
return "Added Successfully"
if not found:
return "No such Transaction"
def TotalSales(self):
mysum=0
with open('Transaction.txt','r') as f:
for line in f:
mysum+=int(line.split()[3])
return mysum
HardwareList=LinkedListHardware()
TransactionList=LinkedListTransaction()
print(TransactionList.TotalSales())
print(TransactionList.remove(input()))
'''
print("Hardware Shop\n")
print("[1]Update hardware item quantity in hand or price per unit\n")
print("[2]Add hardware item\n")
print("[3]Display hardware item\n")
print("[4]Add Sales Transaction\n")
print("[5]Remove Sales Transaction\n")
print("[6]Edit Sales Transaction\n")
print("[7]All Sales Transaction\n")
MainInput=input("Enter Selection: ")
HardwareList=LinkedListHardware()
TransactionList=LinkedListTransaction()
if MainInput=='1':
print("\nUpdate hardware item quantity in hand or price per unit")
print(HardwareList.update(input("Enter Barcode: "),input("Enter Description: "),input("Enter Price: "),input("Enter Quantity: ")))
SubInput=int(input("Enter 0 to exit and 1 to continue "))
if SubInput==0:
quit()
if SubInput==1:
print("Hardware Shop\n")
print("[1]Update hardware item quantity in hand or price per unit\n")
print("[2]Add hardware item\n")
print("[3]Display hardware item\n")
print("[4]Add Sales Transaction\n")
print("[5]Remove Sales Transaction\n")
print("[6]Edit Sales Transaction\n")
print("[7]All Sales Transaction\n")
MainInput=input("Enter Selection: ")
if MainInput=='2':
print("\nAdd hardware item")
print(HardwareList.add(input("Enter Barcode: "),input("Enter Description: "),input("Enter Price: "),input("Enter Quantity: ")))
SubInput=int(input("Enter 0 to exit and 1 to continue "))
if SubInput==0:
quit()
if SubInput==1:
print("Hardware Shop\n")
print("[1]Update hardware item quantity in hand or price per unit\n")
print("[2]Add hardware item\n")
print("[3]Display hardware item\n")
print("[4]Add Sales Transaction\n")
print("[5]Remove Sales Transaction\n")
print("[6]Edit Sales Transaction\n")
print("[7]All Sales Transaction\n")
MainInput=input("Enter Selection: ")
if MainInput=='3':
print("\nDisplay hardware item")
print(HardwareList.display(input("Enter Barcode: ")))
SubInput=int(input("Enter 0 to exit and 1 to continue "))
if SubInput==0:
quit()
if SubInput==1:
print("Hardware Shop\n")
print("[1]Update hardware item quantity in hand or price per unit\n")
print("[2]Add hardware item\n")
print("[3]Display hardware item\n")
print("[4]Add Sales Transaction\n")
print("[5]Remove Sales Transaction\n")
print("[6]Edit Sales Transaction\n")
print("[7]All Sales Transaction\n")
MainInput=input("Enter Selection: ")
if MainInput=='4':
print("\nAdd Sales Transaction")
print(TransactionList.add(input("Enter Invoice Number: "),input("Enter Hardware Barcode: "),input("Enter Quantity: "),input("Enter Total Price: ")))
SubInput=int(input("Enter 0 to exit and 1 to continue "))
if SubInput==0:
quit()
if SubInput==1:
print("Hardware Shop\n")
print("[1]Update hardware item quantity in hand or price per unit\n")
print("[2]Add hardware item\n")
print("[3]Display hardware item\n")
print("[4]Add Sales Transaction\n")
print("[5]Remove Sales Transaction\n")
print("[6]Edit Sales Transaction\n")
print("[7]All Sales Transaction\n")
MainInput=input("Enter Selection: ")
if MainInput=='5':
print("\nRemove Sales Transaction")
print(TransactionList.remove(input("Enter Invoice Number: ")))
SubInput=int(input("Enter 0 to exit and 1 to continue "))
if SubInput==0:
quit()
if SubInput==1:
print("Hardware Shop\n")
print("[1]Update hardware item quantity in hand or price per unit\n")
print("[2]Add hardware item\n")
print("[3]Display hardware item\n")
print("[4]Add Sales Transaction\n")
print("[5]Remove Sales Transaction\n")
print("[6]Edit Sales Transaction\n")
print("[7]All Sales Transaction\n")
MainInput=input("Enter Selection: ")
if MainInput=='6':
print("\nEdit Sales Transaction")
print(TransactionList.update(input("Enter Invoice Number: "),input("Enter Hardware Barcode: "),input("Enter Quantity: "),input("Enter Total Price: ")))
SubInput=int(input("Enter 0 to exit and 1 to continue "))
if SubInput==0:
quit()
if SubInput==1:
print("Hardware Shop\n")
print("[1]Update hardware item quantity in hand or price per unit\n")
print("[2]Add hardware item\n")
print("[3]Display hardware item\n")
print("[4]Add Sales Transaction\n")
print("[5]Remove Sales Transaction\n")
print("[6]Edit Sales Transaction\n")
print("[7]All Sales Transaction\n")
MainInput=input("Enter Selection: ")
if MainInput=='7':
print("\nAll Sales Transaction")
print(TransactionList.TotalSales())
SubInput=int(input("Enter 0 to exit and 1 to continue "))
if SubInput==0:
quit()
if SubInput==1:
print("Hardware Shop\n")
print("[1]Update hardware item quantity in hand or price per unit\n")
print("[2]Add hardware item\n")
print("[3]Display hardware item\n")
print("[4]Add Sales Transaction\n")
print("[5]Remove Sales Transaction\n")
print("[6]Edit Sales Transaction\n")
print("[7]All Sales Transaction\n")
MainInput=input("Enter Selection: ")
'''
The error is receive
mysum+=int(line.split()[3])
IndexError: list index out of range