【发布时间】:2016-02-14 09:39:37
【问题描述】:
那么,我该如何操作一段代码,使其从 .txt 文件中读取数据,并以表格格式打印数据,其中包含“居民编号”、“出租日期”、“价格”等标题?文件名为 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
请注意-
如果支付的金额等于租金金额,则该居民数据不应显示在表格中
如果状态为 N 或 P,则该居民数据也不应显示在表格中
如何显示一个表格(没有导入模块),其标题为“居民编号”“租金日期”“客户编号”“租金金额”“未付金额” - 未付金额只是租金金额 - 从中支付的金额线。另外,我如何打印未付款项的最终总数(所有尚未付款且状态为“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
【问题讨论】:
-
你的意思是内存中的一个表?
-
我不明白你的意思?
-
你想把它存储在内存中的类似表格的结构中,还是想简单地以表格格式打印出来?
标签: python python-3.x tabular