【问题标题】:Reading from a .txt file and printing in a table form从 .txt 文件中读取并以表格形式打印
【发布时间】: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


【解决方案1】:

你可以这样做:

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

【讨论】:

  • 非常感谢!我怎样才能使它在打印表格时更加整洁?就像标题下的所有内容一样,都在正确的标题下
  • 如果你有足够宽的终端,它应该已经这样做了。请参阅我更新的输出答案。该结果来自一个 80 个字符宽的终端。
  • 租金金额和支付金额似乎不符合要求,而且,我怎样才能让所有内容都向左移动,使其与该列的第一个字母一致
  • 对不起;我不知道如何将它们排成一行;为什么他们不这样做对我来说没有意义。不过,我已经更新了我的答案,使其左对齐。
  • 如何添加显示未结金额的列(即 line.split[3] - line.split[5]
【解决方案2】:

你可以使用表格

https://pypi.python.org/pypi/tabulate

来自链接:

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
-----  ------  -------------

【讨论】:

  • 我无法使用 Tabulate,因为客户没有模块,也不愿意得到它。
猜你喜欢
  • 2020-09-18
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多