我正在尝试将文件分类为二维数组。文件看起来像这样
12345 1 893.33
32214 1 343.50
23422 3 903.22
57373 2 893.55
68483 5 329.00
99376 9 329.66
12345 2 999.99
32214 4 892.77
23422 4 895.33
23422 2 492.77
57373 6 985.84
68483 10 1224.90
99376 11 234.56
12345 8 494.44
68483 7 595.55
12345 12 322.22
68483 9 567.85
99376 3 788.77
99376 12 956.66
32214 8 777.77
32214 10 888.88
57373 9 256.56
我当前的代码是此代码,它完全按照外观返回文件,
def print_report(sales_data):
with open(sales_data) as myfile:
sales = myfile.read()
print(sales)
Enter the name of the sales ids file: salesids.txt
Enter the name of the sales data file: salesdata.txt
--------Annual Sales Report--------
ID QT1 QT2 QT3 QT4
12345 1 893.33
32214 1 343.50
23422 3 903.22
57373 2 893.55
68483 5 329.00
99376 9 329.66
12345 2 999.99
32214 4 892.77
23422 4 895.33
23422 2 492.77
57373 6 985.84
68483 10 1224.90
99376 11 234.56
12345 8 494.44
68483 7 595.55
12345 12 322.22
68483 9 567.85
99376 3 788.77
99376 12 956.66
32214 8 777.77
32214 10 888.88
57373 9 256.56
我需要对列表进行排序,以使每个ID(12345)旁边有正确的月份的销售额都一直在右侧,并按正确的季度排序(QT1 = 1-3 QT2 = 4- 6 QT3 = 7-9 QT4 = 10-12
我如何做到这一点?
应该看起来像这样
答案 0 :(得分:0)
您可以在纯Python中尝试以下解决方案:
def getQuarter(q):
q = int(q)
if 1 <= q <= 3:
return 1
elif 4 <= q <= 6:
return 2
elif 7 <= q <= 9:
return 3
else:
return 4
def printHeaders():
print("--- Annual sales report ---")
print("ID\t\t\t",end='')
print("QT1\t\t\t",end='')
print("QT2\t\t\t",end='')
print("QT3\t\t\t",end='')
print("QT4\t\t\t",end='')
print("TOTAL\t\t\t")
def printData(currentId, q1, q2, q3, q4, total):
print(currentId+'\t\t',end='')
print('{0:.2f}'.format(q1)+'\t\t',end='')
print('{0:.2f}'.format(q2)+'\t\t',end='')
print('{0:.2f}'.format(q3)+'\t\t',end='')
print('{0:.2f}'.format(q4)+'\t\t',end='')
print('{0:.2f}'.format(total)+'\t\t')
def printFinal(finalQ1, finalQ2, finalQ3, finalQ4, finalTotal):
print("Total\t\t",end='')
print('{0:.2f}'.format(finalQ1)+"\t\t",end='')
print('{0:.2f}'.format(finalQ2)+"\t\t",end='')
print('{0:.2f}'.format(finalQ3)+"\t\t",end='')
print('{0:.2f}'.format(finalQ4)+"\t\t",end='')
print('{0:.2f}'.format(finalTotal)+"\t\t")
def printMaxSalesperson(maxSalesman):
print("Max sales by salesperson: ID = " + maxSalesman[0] + ", Amount = $" + '{0:.2f}'.format(maxSalesman[1]))
def printMaxQuarterSales(maxQ):
print("Max sales by quarter: Quarter = " + maxQ[1] + ", Amount = $" + '{0:.2f}'.format(maxQ[0]))
def getListData(inFile):
data = [elem.split() for elem in sorted([line for line in inFile], key=lambda elem: elem[0])]
ids = sorted(set([elem[0] for elem in data]))
newData = [[elem for elem in data if elem[0]==item] for item in ids]
return newData
def updateSales(sales,elem,q1,q2,q3,q4):
sales.append([elem[0][0], round(max([q1,q2,q3,q4]),2)])
finalQ1 = finalQ2 = finalQ3 = finalQ4 = finalTotal = 0
sales = []
with open("data.txt", "r") as inFile:
newData = getListData(inFile)
printHeaders()
for elem in newData:
q1 = q2 = q3 = q4 = total = 0.00
currentId = elem[0][0]
for item in elem:
quarter = getQuarter(item[1])
if quarter == 1:
q1 += float(item[2])
elif quarter == 2:
q2 += float(item[2])
elif quarter == 3:
q3 += float(item[2])
else:
q4 += float(item[2])
updateSales(sales,elem,q1,q2,q3,q4)
total = q1 + q2 + q3 + q4
finalQ1 += q1
finalQ2 += q2
finalQ3 += q3
finalQ4 += q4
finalTotal += total
printData(currentId, q1, q2, q3, q4, total)
printFinal(finalQ1, finalQ2, finalQ3, finalQ4, finalTotal)
maxSalesman = max(sales, key=lambda k: k[1])
maxQ = max([finalQ1, '1'],[finalQ2, '2'],[finalQ3, '3'],[finalQ4, '4'], key=lambda elem: elem[0])
printMaxSalesperson(maxSalesman)
printMaxQuarterSales(maxQ)
上面的输出将是:
--- Annual sales report ---
ID QT1 QT2 QT3 QT4 TOTAL
12345 1893.32 0.00 494.44 322.22 2709.98
23422 1395.99 895.33 0.00 0.00 2291.32
32214 343.50 892.77 777.77 888.88 2902.92
57373 893.55 985.84 256.56 0.00 2135.95
68483 0.00 329.00 1163.40 1224.90 2717.30
99376 788.77 0.00 329.66 1191.22 2309.65
Total 5315.13 3102.94 3021.83 3627.22 15067.12
Max sales by salesperson: ID = 12345, Amount = $1893.32
Max sales by quarter: Quarter = 1, Amount = $5315.13
请随时提出任何问题,或者是否需要对代码进行澄清。