使用计数器从csv读取和绘图

时间:2017-11-06 07:00:49

标签: python csv count grouping

您好我有一个csv,其中包含了从1992年至2016年经过维护的25座桥梁的数据。

Bridge.csv

第一列对应于桥号。接下来的25列对应于1992年至2016年的条件等级。如果条件等级增加,则意味着桥梁进行了维护。 现在我已经弄清楚如何获得每个桥接器进行维护的次数,我必须创建一个输入,允许用户输入1992 - 2016年的任何一年,然后打印出桥梁的总数。维护次数增加。我相信这将是一个单独的" if"这样的声明最后的声明。

f = open("BridgeExam1.csv", "r")
year = int(input("Please give a year between 1992 - 2016: "))

for line in f:   
    temp = line.split(",")
    i = 1
    maintenance = 0
    while i < len(temp) - 1:
        if  int(temp[i]) < int(temp[i+1]):
            maintenance+=1 
        i = i+1 
        temp[0]=temp[0] + 1992
    print("Bridge "+ temp[0], maintenance)                                      
f.close()

1 个答案:

答案 0 :(得分:1)

我认为应该这样做。输出桥接编号及其进行维护的次数

f = open("BridgeExam1.csv", "r")
for line in f:   
    temp = line.split(",")
    i = 1
    maintenance = 0
    while i < len(temp) - 1:
        if  int(temp[i]) < int(temp[i+1]):
            maintenance+=1 
        i = i+1 
    print("Bridge "+ temp[0], maintenance)                                      
f.close()
相关问题