【发布时间】:2020-12-01 11:46:35
【问题描述】:
我编写了以下代码,它从 csv 文件中获取一列,然后将其转换为整数并将它们全部相加。我只为一个文件做了这个,我有大约 80 个文件可以应用相同的代码。
import csv
from collections import defaultdict
columns = defaultdict(list)
with open('Team11BoM.csv') as f:
reader = csv.DictReader(f)
for row in reader:
for (k,v) in row.items():
if k not in columns:
columns[k] = list()
columns[k].append(v)
import pandas as pd
df = pd.read_csv("Team11BoM.csv")
b = list(df['Reported Price'])
a = list(df['Actual Price'])
for i in range(0, len(a)):
a[i] = int(float(a[i]))
v = sum(a)
print("the total actual cost(s) for team 11 is:", v)
for i in range(0, len(b)):
b[i] = int(float(b[i]))
h = sum(b)
print("the total reported price for team 11 is:", h)
它打印出以下内容:
the total actual cost(s) for team 11 is: 945
the total reported price for team 11 is: 707
我希望它打印出来:
the total actual cost(s) for *filename* is: *Total cost of that team*
the total reported price for *filename* is: *Total reported price of that team*
有什么简单的方法吗?
谢谢, 伊尔凡 S.
【问题讨论】:
-
我为您的问题添加了答案。
标签: python python-3.x pandas csv file