2016-04-27 69 views
0

我在從Python上創建的表中添加某些值時遇到了一些麻煩。
我的文本文件看起來像:從文本文件創建表格並添加列

E5345,22/09/2015,C106,815,A,400 
E5348,23/09/2015,C109,370,A,200 
E5349,25/09/2015,C110,480,A,250 
E5353,28/09/2015,C114,272,A,200 
E5363,01/10/2015,C124,930,A,500 
E5364,02/10/2015,C125,915,A,800 
E5365,02/10/2015,C126,1486,A,1486 
E5366,03/10/2015,C127,576,E,0 
E5367,03/10/2015,C128,427,A,350 
E5373,10/10/2015,C134,1023,A,550 

我希望創建使用這些值在蟒蛇一個表,並創建另一個列,標題爲優秀金額。我已經設法做到這一點,但我想找到未付款的總和。這是通過執行第四列(​​finaltotal)減去第六列而找到的。 (amountpaid)。
到目前爲止我的代碼是:

headers = ["| Estimate Number", "| Date", "| Customer Number", "| Final Total", "| Status", "| Amount Paid", "| Outstanding Amount"] 
print(" ".join(headers)) 
print ("\n-------------------------------------------------------------------------------------------------------------------------") 
data = open("paintingJobs.txt", "r") 
info=data.readlines() 
data.close() 
for li in info: 
     line = li.strip().split(",") 
     status=line[4] 
     finaltotal=int(line[3]) 
     amountpaid=int(line[5]) 
     subtotal = int(line[3]) - int(line[5]) 
     outstanding = (finaltotal) - (amountpaid) 
     line.append(str(subtotal)) 
     if (amountpaid) < (finaltotal) and status == "A": 
       for i, word in enumerate(line): 
        print(word.ljust(len(headers[i - (i > 20)])), end="  " * ((i - (i > 20)) != len(headers) - 1)) 
       print() 
       print ("-------------------------------------------------------------------------------------------------------------------------") 

    print ("\nThe total revenue is") 

我的理想輸出爲:

| Estimate Number | Date | Customer Number | Final Total | Status | Amount Paid | Outstanding Amount 

------------------------------------------------------------------------------------------------------------------------- 
E5345     22/09/2015  C106     815    A   400    415 
------------------------------------------------------------------------------------------------------------------------- 
E5348     23/09/2015  C109     370    A   200    170 
------------------------------------------------------------------------------------------------------------------------- 
E5349     25/09/2015  C110     480    A   250    230 
------------------------------------------------------------------------------------------------------------------------- 
E5353     28/09/2015  C114     272    A   200    72 
------------------------------------------------------------------------------------------------------------------------- 
E5355     29/09/2015  C116     530    A   450    80 
------------------------------------------------------------------------------------------------------------------------- 
E5363     01/10/2015  C124     930    A   500    430 
------------------------------------------------------------------------------------------------------------------------- 
E5364     02/10/2015  C125     915    A   800    115 
------------------------------------------------------------------------------------------------------------------------- 
E5367     03/10/2015  C128     427    A   350    77 
------------------------------------------------------------------------------------------------------------------------- 
E5373     10/10/2015  C134     1023    A   550    473 
------------------------------------------------------------------------------------------------------------------------- 

The total outstanding is 
£2062 

outstanding通過找到outstanding量的總和找到。未付金額爲finaltotal減去amountpaid。任何幫助將非常感激。

+0

你有什麼問題? –

+0

我想找到最後一列(未付金額)的總和並打印出來。它應該等於2062 – Seruza

回答

1

只需在循環外使用一個變量來跟蹤總數。我不包括那些多付的人,但是如果您想從總額中扣除,您可以刪除if聲明。

total = 0 
for li in info: 
    ... 
    outstanding = (finaltotal) - (amountpaid) 
    if outstanding > 0: 
     total += outstanding 

print 'The total outstanding is {}'.format(total) 
1

爲什麼不使用熊貓圖書館?假設您的初始表位於名爲example.csv的csv文件中。您可以通過幾個步驟完成此操作,例如:

from pandas import read_csv 

df = read_csv('~/Documents/example.csv', header=None) 
df.columns = ["Estimate Number", "Date", "Customer Number", "Final Total", "Status", "Amount Paid"] 
df['Outstanding Amount'] = df['Final Total'] - df['Amount Paid'] 

然後您可以選擇輸出。你可以輸出回到一個CSV文件:

df.to_csv() 

或打印:

table_as_text = df.to_string() 
print(table_as_text) 

,這將給你:

Estimate Number  Date Customer Number Final Total Status Amount Paid Outstanding Amount 
0   E5345 22/09/2015   C106   815  A   400     415 
1   E5348 23/09/2015   C109   370  A   200     170 
2   E5349 25/09/2015   C110   480  A   250     230 
3   E5353 28/09/2015   C114   272  A   200     72 
4   E5363 01/10/2015   C124   930  A   500     430 
5   E5364 02/10/2015   C125   915  A   800     115 
6   E5365 02/10/2015   C126   1486  A   1486     0 
7   E5366 03/10/2015   C127   576  E   0     576 
8   E5367 03/10/2015   C128   427  A   350     77 
9   E5373 10/10/2015   C134   1023  A   550     473 

並計算總和,你會怎麼做:

outstanding_amount = df["Outstanding Amount"].sum() 
print(outstanding_amount) 

結帳http://pandas.pydata.org/pandas-docs/stable。希望有所幫助!