[英]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.