从 .txt 文件读取并以 table 格式打印
Reading from a .txt file and printing in a table form
那么我该如何操作一段代码,以便它从 .txt 文件中读取数据,并以 table 格式打印数据,标题如 'Resident Number' 'Rent date' 'Price'等等?文件的名称是 Residents.txt
到目前为止,我有这个
file = open('Residents.txt','r')
For line in file:
SplitFile = line.split (',')
这是 .txt 文件-
R1,21/09/2015,C1,440,P,0
R2,21/09/2015,C3,290,A,290
R3,21/09/2015,C4,730,N,0
R4,22/09/2015,C5,180,A,180
R5,22/09/2015,C6,815,A,400
R6,23/09/2015,C7,970,N,0
R7,23/09/2015,C8,1050,P,0
R8,23/09/2015,C9,370,A,200
R9,25/09/2015,C10,480,A,250
R10,25/09/2015,C11,330,A,330
这是 .txt 文件中每一列的表示-
line.split[0] = Resident Number
line.split[1] = Rent Date
line.split[2] = Customer number
line.split[3] = Rent amount
line.split[4] = (A means Accepted)(N means not accepted)(P means Pending)
line.split[5] = Amount paid
请注意-
如果支付的金额等于租金金额,则居民数据不应显示在 table
中
如果状态为 N 或 P,则居民数据也不应显示在 table
中
如何显示标题为 'Resident number' 'Rent date' 'Customer number' 'Rent amount' 'Amount outstanding'- 的 table(不导入模块)未付金额只是租金金额 - 从该行支付的金额。另外,我怎样才能打印出未结金额的最终总额(所有尚未付款且状态为 'A' 的人的总和)
Python 3.5
谢谢
编辑
for i, word in enumerate(line):
if i == 4 : # We don't print the status
continue
elif i == 2:
continue
print(word.ljust(len(headers[i - (i > 4)(i > 2)])), end=" " * ((i - (i > 4)(i > 2)) != len(headers) - 1))
print()
进一步编辑 (15/02)
line = line.strip().split(",")
subtotal = int(line[3]) - int(line[5])
line.append(str(subtotal))
if line[5] == line[3] or line[4] in ("N", "E"):
continue
total += subtotal
您可以使用表格
https://pypi.python.org/pypi/tabulate
来自 link:
from tabulate import tabulate
table = [["Sun",696000,1989100000],["Earth",6371,5973.6],
["Moon",1737,73.5],["Mars",3390,641.85]]
print tabulate(table)
输出:
----- ------ -------------
Sun 696000 1.9891e+09
Earth 6371 5973.6
Moon 1737 73.5
Mars 3390 641.85
----- ------ -------------
你可以这样做:
headers = ["Resident Number", "Rent Date", "Customer number", "Rent ammount", "Ammount paid", "Outstanding Ammount"]
print(" ".join(headers))
for line in open("test.txt", "r"):
line = line.strip().split(",")
line.append(str(int(line[3]) - int(line[5])))
if line[5] == line[3] or line[4] in ("N", "P"):
continue
for i, word in enumerate(line):
if i == 4:
continue
print(word.ljust(len(headers[i - (i > 4)])), end=" " * ((i - (i > 4)) != len(headers) - 1))
print()
输出:
Resident Number Rent Date Customer number Rent ammount Ammount paid Outstanding Ammount
R5 22/09/2015 C6 815 400 415
R8 23/09/2015 C9 370 200 170
R9 25/09/2015 C10 480 250 230
那么我该如何操作一段代码,以便它从 .txt 文件中读取数据,并以 table 格式打印数据,标题如 'Resident Number' 'Rent date' 'Price'等等?文件的名称是 Residents.txt
到目前为止,我有这个
file = open('Residents.txt','r')
For line in file:
SplitFile = line.split (',')
这是 .txt 文件-
R1,21/09/2015,C1,440,P,0
R2,21/09/2015,C3,290,A,290
R3,21/09/2015,C4,730,N,0
R4,22/09/2015,C5,180,A,180
R5,22/09/2015,C6,815,A,400
R6,23/09/2015,C7,970,N,0
R7,23/09/2015,C8,1050,P,0
R8,23/09/2015,C9,370,A,200
R9,25/09/2015,C10,480,A,250
R10,25/09/2015,C11,330,A,330
这是 .txt 文件中每一列的表示-
line.split[0] = Resident Number
line.split[1] = Rent Date
line.split[2] = Customer number
line.split[3] = Rent amount
line.split[4] = (A means Accepted)(N means not accepted)(P means Pending)
line.split[5] = Amount paid
请注意-
如果支付的金额等于租金金额,则居民数据不应显示在 table
中
如果状态为 N 或 P,则居民数据也不应显示在 table
如何显示标题为 'Resident number' 'Rent date' 'Customer number' 'Rent amount' 'Amount outstanding'- 的 table(不导入模块)未付金额只是租金金额 - 从该行支付的金额。另外,我怎样才能打印出未结金额的最终总额(所有尚未付款且状态为 'A' 的人的总和)
Python 3.5
谢谢
编辑
for i, word in enumerate(line):
if i == 4 : # We don't print the status
continue
elif i == 2:
continue
print(word.ljust(len(headers[i - (i > 4)(i > 2)])), end=" " * ((i - (i > 4)(i > 2)) != len(headers) - 1))
print()
进一步编辑 (15/02)
line = line.strip().split(",")
subtotal = int(line[3]) - int(line[5])
line.append(str(subtotal))
if line[5] == line[3] or line[4] in ("N", "E"):
continue
total += subtotal
您可以使用表格
https://pypi.python.org/pypi/tabulate
来自 link:
from tabulate import tabulate
table = [["Sun",696000,1989100000],["Earth",6371,5973.6],
["Moon",1737,73.5],["Mars",3390,641.85]]
print tabulate(table)
输出:
----- ------ -------------
Sun 696000 1.9891e+09
Earth 6371 5973.6
Moon 1737 73.5
Mars 3390 641.85
----- ------ -------------
你可以这样做:
headers = ["Resident Number", "Rent Date", "Customer number", "Rent ammount", "Ammount paid", "Outstanding Ammount"]
print(" ".join(headers))
for line in open("test.txt", "r"):
line = line.strip().split(",")
line.append(str(int(line[3]) - int(line[5])))
if line[5] == line[3] or line[4] in ("N", "P"):
continue
for i, word in enumerate(line):
if i == 4:
continue
print(word.ljust(len(headers[i - (i > 4)])), end=" " * ((i - (i > 4)) != len(headers) - 1))
print()
输出:
Resident Number Rent Date Customer number Rent ammount Ammount paid Outstanding Ammount
R5 22/09/2015 C6 815 400 415
R8 23/09/2015 C9 370 200 170
R9 25/09/2015 C10 480 250 230