挑出仪器编号头尾的Python程序

来源:互联网 发布:gta5第一人称视角优化 编辑:程序博客网 时间:2024/04/28 22:17

设备仪器号在第一列,插入后面为结束编号,功能很好用。

先将Excel按仪器编号排序,再换为csv格式,按名称提取首尾编号后,再写回CSV格式。

这是我的第二个python解决实际问题的程序。

f=open(r'c:\gyjj.csv')#计算读入了多少行 python 3.3.2n=0for line in f:    n+=1f.close()print ("rows if file:",n)#countf=open(r'c:\gyjj.csv')fo=open(r'c:\公寓家具编号汇总.csv','w')title=f.readline().strip('\n').split(',')print ("标题行:",title)title.insert(1,"终止编号")fo.write(','.join(title)+'\n')#统计安排量与起止编号t=f.readline().strip('\n').split(',')print("编号,仪器名称:",t[0],t[2])for i in range(n-2):    #print ("time:",i)    nextrows=f.readline().strip('\n').split(',')    if  t[2]==nextrows[2]:        endno=nextrows[0]        continue    else:        print("编号",t[0],endno)        wd=t[:]        wd.insert(1,endno)        fo.write(','.join(wd)+'\n')        t=nextrows        endno=nextrows[0]print("最后:",t[0],nextrows[0])wd=t[:]wd.insert(1,endno)fo.write(','.join(wd))        #关闭文件fo.close()f.close()


原创粉丝点击