我只是导入一个csv文件并将其转换为字典,还为不同的列分配了字段名称。有人可以教我如何循环添加此字典中相同键的值吗?
import csv
with open("breast_cancer_v1.csv", newline='') as file:
fieldnames = ['1', '2','3','4','5','6','7','8','9','10','11']
print ("1: ID", "\n2: Diagnosis", "\n3: a) radius (mean of distances from center to points on the perimeter)", "\n4: b) texture (standard deviation of gray-scale values)", "\n5: c) perimeter", "\n6: d) area", "\n7: e) smoothness (local variation in radius lengths)", "\n8: f) compactness (perimeter^2 / area - 1.0)", "\n9: g) concavity (severity of concave portions of the contour)", "\n10: h) concave points (number of concave portions of the contour)", "\n11: i) symmetry", "\n12: j) fractal dimension (coastline approximation - 1)")
csv_file = csv.DictReader(file, fieldnames)
for row in csv_file:
print(row)
示例:我希望将“ 3”列中的所有值相加或计算“ 2”列中有多少“ B”。仍在学习如何访问字典。导入csv后,我设法在循环中打印出了我的字典,看起来像这样: