like this
Shopping report
Name1
Thing1 number1 cost1
Thing11 number11 cost11 the amount to be paid
Name2
Thing2 number2 cost2
Thing22 number22 cost22
the amount to be paid
text file
Petrov pen 10 151000
Borzov paper 20 20000
Butar case-boks 5 5000
Semerik pen2 500 250000
Petrov pen(oil) 100 10000
Borzov paper 40 40000
Butar book 5 15000
Semerik hat 500 250000
CodePudding user response:
You would do it like this:
path = "C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python38\\Stack overflow\\textfile.txt"
newpath = "C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python38\\Stack overflow\\textfileout.txt"
text = open(path, "r")
textinfo = text.readlines()
dictofinfo = {}
for line in textinfo:
line = line.strip()
lineaslist = line.split(" ")
name = lineaslist[0]
if not(name in dictofinfo.keys()):
dictofinfo[lineaslist[0]] = [lineaslist[1:]]
else:
dictofinfo[lineaslist[0]].append(lineaslist[1:])
text.close()
out = open(newpath, "w")
for entry in dictofinfo:
totaltobepayed = 0
out.write(entry "\n")
for seperateentry in dictofinfo[entry]:
totaltobepayed = int(seperateentry[2])
out.write(" ".join(seperateentry) "\n")
out.write(str(totaltobepayed) "\n")
out.close()
Replace the path variable with the path to your text file.