Skip to main content
 首页 » 编程设计

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

2025年01月19日10zhengyun_ustc

你好,我有一个 csv,其中包含 1992 年至 2016 年进行维护的 25 座桥梁的数据。

第一列对应桥号。接下来的 25 列对应于 1992 年至 2016 年的状况等级。如果状况等级增加,则意味着桥梁进行了维护。 既然我已经弄清楚如何获取每座桥梁进行维护的次数,我必须创建一个输入,允许用户输入 1992 年至 2016 年的任何一年,然后打印维护次数增加的桥梁总数。我相信这将是一个单独的“如果”声明。

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() 

请您参考如下方法:

我认为应该这样做。输出桥号和维护次数

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()