簡體   English   中英

如果不使用 panda 或 numpy,我如何將數據分成偶數列? 然后總結最后一列

[英]Without using panda or numpy, how would I separate my data into even columns? Then sum up the last column

我正在嘗試對齊年份、model 和價格。 然后總結成本。

未轉換為 csv 或使用 panda 和 numpy 保存為以下“car.dat”數據。

2018 年,阿爾法羅密歐 Stelvio Ti,28,950 美元

2022,梅賽德斯-奔馳 E350,54,950 美元

2022,沃爾沃XC90,49,900

到目前為止,我使用的是 python idle shell 3.10.4:

def main():
    file  = open('car.dat','r')
    content = file.readlines()
    #total =0
    #total =  all 3 rows.
    for line in content:
        print ('Year' 'Make/Model', 'Price')
        print ('-'*35)
        line = line.split('\n')
        print(line[0].replace(',',' ',2))
        #print ('Total'+ total)
main()

預期結果如下:

在此處輸入圖像描述

也許類似於此:

def main():
    total = 0
    print("Year".ljust(6), "Make/Model".ljust(10), "Price")
    print("-" * 35)
    with open('car.dat') as f:
        for line in f:
            year, make_model, price = line.split(",")
            print(year.ljust(6), make_model.ljust(10), price)
            total += int(price)
    print("-" * 35)
    print("total".rjust(16), str(total))

if __name__ == "__main__":
    main()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM