Created
May 10, 2018 05:59
-
-
Save Sleepful/698657f8b6a6bc4ecaa98a3fe5fc680d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#exec(open("C:/Users/Pablo.PABLO-XPS/Documents/python/billscript.py").read()) | |
filepath = "C:/Users/Pablo.PABLO-XPS/Documents/python/" | |
filename = "april" | |
fileext = ".txt" | |
bills = {} | |
class Bill: | |
def __init__(self, price, details): | |
self.price = price | |
self.details = details | |
def billsMaxToMin(): | |
li = [] | |
for c in bills: | |
for b in bills[c]: | |
li.append([b.price, c, b.details]) | |
li.sort() | |
li.reverse() | |
return li | |
def billsPerCat(): | |
li = [] | |
for c in bills: | |
total = 0 | |
for b in bills[c]: | |
total += b.price | |
li.append([total, c]) | |
li.sort() | |
li.reverse() | |
return li | |
filedst = open(filepath+filename+"_result"+fileext,"w") | |
filesrc = open(filepath+filename+fileext, "r") | |
#parsing | |
for line in filesrc: | |
#print(line) | |
#filedst.write(line) | |
line = line.replace('\n','') | |
parts = line.split(" ", 2) | |
if len(parts)<2: | |
continue | |
price = int(parts[0]) | |
category = parts[1] | |
try: | |
details = parts[2] | |
except IndexError: | |
details = '' | |
b = Bill(price=price, details=details) | |
if category not in bills: | |
bills[category] = [] | |
bills[category].append(b) | |
#writting | |
maxtomin=billsMaxToMin() | |
print(maxtomin) | |
filedst.write("Max to min todos los recibos\n------------------------\n\n") | |
for item in maxtomin: | |
string = str(item[0]) + " " + item[1] | |
if item[2] != "": | |
string += " > " + item[2] | |
string += "\n" | |
filedst.write(str(string)) | |
filedst.write("\n\n") | |
#writting | |
billspercat=billsPerCat() | |
print(billspercat) | |
filedst.write("Total por categoria\n------------------\n\n") | |
for item in billspercat: | |
string = str(item[0]) + " > " + item[1] | |
string += "\n" | |
filedst.write(str(string)) | |
filedst.write("\n\n") | |
#writting | |
filedst.write("Gran total\n------------------\n\n") | |
total = 0 | |
for item in billspercat: | |
total += item[0] | |
string = str(total) + "\n" | |
filedst.write(str(string)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment