读取文件和打印输出

时间:2015-01-25 00:34:20

标签: python python-3.x

我正在读取一个文件,输出应该看起来像下面那个,忽略实际表格我的小时,分​​钟和秒的值都是关闭的钱,这应该通过四舍五入来计算分钟;我已经尝试了很多方法来解决这个问题,这是我的最后一招。

+--------------+------------------------------+---+---------+--------+
| Phone number | Name                         | # |Duration | Due    |
+--------------+------------------------------+---+---------+--------


|(780) 123 4567|Ameneh Gholipour Shahraki     |384|55h07m53s|$ 876.97|**
|(780) 123 6789|Stuart Johnson                |132|17h53m19s|$ 288.81|
|(780) 321 4567|Md Toukir Imam                |363|49h52m12s|$ 827.48|++
|(780) 432 1098|Hamman Samuel                 |112|16h05m09s|$ 259.66|
|(780) 492 2860|Osmar Zaiane                  |502|69h27m48s|$1160.52|**
|(780) 789 0123|Elham Ahmadi                  |259|35h56m10s|$ 596.94|
|(780) 876 5432|Amir Hossein Faghih Dinevari  |129|17h22m32s|$ 288.56|
|(780) 890 7654|Weifeng Chen                  |245|33h48m46s|$ 539.41|
|(780) 987 6543|Farrukh Ahmed                 |374|52h50m11s|$ 883.72|**
+--------------+------------------------------+---+---------+--------+
| Total dues   |                                          $   5722.07|
+--------------+-----------------------------------------------------+

这是我的代码,我在使用time()和due()函数时遇到的麻烦最多

from collections import Counter

customers=open('customers.txt','r')
calls=open('calls.txt.','r')

def main():
    customers=open('customers.txt','r')
    calls=open('calls.txt.','r')    
    print("+--------------+------------------------------+---+---------+--------+")
    print("| Phone number | Name                         | # |Duration | Due    |")
    print("+--------------+------------------------------+---+---------+--------+")
    phone_sorter() 
    number_calls()
    time()
    due()
def phone_sorter():
    sorted_no={}
    for line in customers:
        rows=line.split(";")
        sorted_no[rows[1]]=rows[0]
    for value in sorted(sorted_no.values()):
            for key in sorted_no.keys():
                if sorted_no[key] == value:
                    print(sorted_no[key],key)

def number_calls():
    no_calls={}
    for line in calls:
        rows=line.split(";")
        if rows[1] not in no_calls:
            no_calls[rows[1]]=1
        else:
            no_calls[rows[1]]+=1

    s=sorted(no_calls.keys())
    for key in s:
        print(no_calls[key])


def time():
    calls=open('calls.txt.','r')  
    n_list=[]
    d={}
    for line in calls:
        rows=line.split(";")
        d[rows[1]]=rows[3]
        if rows[1] not in d:
            d[rows[1]]=rows[3]
        else:
            d[rows[1]]+=rows[3]

    x=sorted(d.keys())

    for value in x:
        m, s = divmod(int(value), 60)
        h, m = divmod(m, 60)
        print("%d:%02d:%02d" % (h, m, s)) 

def due():
    calls=open('calls.txt.','r')
    d2={}
    for line in calls:
        rows=line.split(";")
        d2[rows[1]]=float(rows[3])*float(rows[4])
        if rows[1] not in d2:
            d2[rows[1]]=float(rows[3])*float(rows[4])
        else:
            d2[rows[1]]+=float(rows[3])*float(rows[4])
    x=sorted(d2.keys())
    for key in x:
        print(d2[key])
    print(sum(d2.values()))

main()

这是我在pastebin中阅读的文件的链接:http://pastebin.com/RSMnXDtq

第一列是电话号码。此编号必须格式化为(999)999 9999。

第二列是名称,宽度必须为30个字符。

第三列是来自相关手机的来电数量。它应该是3位数。

第四列是来自相关电话的来电总持续时间。此持续时间应格式如下:99h99m99s小时,分钟和秒。如果小于10,则分钟和秒的前缀应为0。

第五列是根据每次通话所附的费率计算的通话金额。请注意,每次通话的持续时间应向上舍入到分钟,以便使用每分钟的费率。此金额应打印7个位置,小数点后仅2个。

1 个答案:

答案 0 :(得分:0)

以下是使用pandas的解决方案:

from pandas import np, read_csv

#
# Load and process call data
#

def get_data(calls_fname, custs_fname):
    # load call data
    calls = read_csv(
        calls_fname,
        sep    = ";",
        names  = ["session", "phone", "to", "seconds", "rate"],
        header = None
    )    
    # calculate cost per call (time rounded up to the next minute)
    calls["cost"] = np.ceil(calls["seconds"] / 60.) * calls["rate"]
    # add a call-count column
    #   (I think there is a better way to do this using np.size in
    #   the .groupby, but I haven't been able to figure it out)
    calls["count"] = 1
    # find per-cust totals
    calls = calls.groupby(["phone"]).agg({"seconds":np.sum, "cost":np.sum, "count":np.sum})
    # load customer data
    custs = read_csv(
        custs_fname,
        sep       = ";",
        names     = ["phone", "name"],
        header    = None,
        index_col = 0   # index by phone number
    )
    # join to find customer name
    return calls.join(custs, sort=False).reset_index()

#
# output formatting functions
#

def phone_str(i):
    """
    Convert int 1234567890 to str "(123) 456 7890"
    """
    s = str(i).zfill(10)
    return "({}) {} {}".format(s[0:3], s[3:6], s[6:10])

def time_str(i):
    """
    Convert int 3662 to str " 1h01m02s"
    """
    m, s = divmod(i, 60)
    h, m = divmod(m, 60)
    return "{:>2d}h{:02d}m{:02d}s".format(h, m, s)

def make_table(totals):
    header = (
        "+--------------+------------------------------+---+---------+--------+\n"
        "| Phone number | Name                         | # |Duration | Due    |\n"
        "+--------------+------------------------------+---+---------+--------+\n"
    )
    rows = [
        "|{}|{:<30}|{:>3d}|{}|${:7.2f}|\n"
        .format(
            phone_str(row["phone"  ]),
                      row["name"   ],
                      row["count"  ],
            time_str (row["seconds"]),
                      row["cost"   ]
        )
        for i,row in totals.iterrows()
    ]
    total_dues = np.sum(totals["cost"])
    footer = (
        "+--------------+------------------------------+---+---------+--------+\n"
        "| Total dues   |                                          ${:10.2f}|\n"
        "+--------------+-----------------------------------------------------+"
        .format(total_dues)
    )
    return header + "".join(rows) + footer

def main():
    totals = get_data("calls.txt", "customers.txt")
    print(make_table(totals))

if __name__ == "__main__":
    main()

将您的pastebin链接中的数据用作calls.txt,将以下内容用作customers.txt

7801236789;Stuart Johnson
7804321098;Hamman Samuel
7803214567;Md Toukir Imam
7804922860;Osmar Zaiane
7801234567;Ameneh Gholipour Shahraki
7807890123;Elham Ahmadi
7808765432;Amir Hossein Faghih Dinevari
7808907654;Weifeng Chen
7809876543;Farrukh Ahmed

它产生

+--------------+------------------------------+---+---------+--------+
| Phone number | Name                         | # |Duration | Due    |
+--------------+------------------------------+---+---------+--------+
|(780) 123 4567|Ameneh Gholipour Shahraki     |384|55h07m53s|$ 876.97|
|(780) 123 6789|Stuart Johnson                |132|17h53m19s|$ 288.81|
|(780) 321 4567|Md Toukir Imam                |363|49h52m12s|$ 827.48|
|(780) 432 1098|Hamman Samuel                 |112|16h05m09s|$ 259.66|
|(780) 492 2860|Osmar Zaiane                  |502|69h27m48s|$1160.52|
|(780) 789 0123|Elham Ahmadi                  |259|35h56m10s|$ 596.94|
|(780) 876 5432|Amir Hossein Faghih Dinevari  |129|17h22m32s|$ 288.56|
|(780) 890 7654|Weifeng Chen                  |245|33h48m46s|$ 539.41|
|(780) 987 6543|Farrukh Ahmed                 |374|52h50m11s|$ 883.72|
+--------------+------------------------------+---+---------+--------+
| Total dues   |                                          $   5722.07|
+--------------+-----------------------------------------------------+