本代码是利用读取的csv获取需要新增的ini信息,再读取ini,在原有的ini文件后面新增信息,但是不能在文件的后面插入。
中间涉及到字符串与日期时间类型的转换
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 3 13:32:25 2021
@author: 10+1
"""
import ConfigParser
import csv
import datetime
#本程序可以负责利用csv表格生成对应ini及csv记录表,修改一共四个路径
csv_file=csv.reader(open('H:\\mission\\satelliateJiashan\\test2.csv','r'))
cfg1 = "H:\\mission\\satelliateJiashan\\TEST.ini"
conf = ConfigParser.ConfigParser()
conf.read(cfg1)
content=[]
sections=conf.sections()
maxsections=int(sections[0])
for line in csv_file:
print(line) #打印文件每一行的信息
#时间的处理
dateTime_s = datetime.datetime.strptime(line[1],'%Y-%m-%d %H:%M:%S')+datetime.timedelta(seconds=-20)
dateTime_e = datetime.datetime.strptime(line[2],'%Y-%m-%d %H:%M:%S')+datetime.timedelta(seconds=20)
str_s=dateTime_s.strftime("%Y.%m.%d %H:%M:%S")
str_e=dateTime_e.strftime("%Y.%m.%d %H:%M:%S")
print line[0],str_s,str_e
conf.add_section(str(maxsections+1))
maxsections=maxsections+1
if line[0]=='SUOMINPP':
line[0]='NPP'
conf.set(str(maxsections),"description",line[0])
conf.set(str(maxsections),"start",str_s)
conf.set(str(maxsections),"stop",str_e)
address=''
if line[0]=='AQUA':
address='C:/Program Files/ALMUR/bin/INI/AQUA_config.ini'
elif line[0]=='NOAA20':
address='C:/Program Files/ALMUR/bin/INI/NOAA20.ini'
elif line[0]=='TERRA':
address='C:/Program Files/ALMUR/bin/INI/Terra_720_DataPath-D-Alex.ini'
#其他的卫星需要自行添加
conf.set(str(maxsections),"config",address)
print line[0],str_s,str_e,address
line[1]=str_s
line[2]=str_e
line.append(address)
content.append(line)
#输出文件csv,为了腾讯表格
csvfile = open('H:\\mission\\satelliateJiashan\\test2recorder.csv', 'wb') #打开方式还可以使用file对象
writer = csv.writer(csvfile)
writer.writerows(content)
csvfile.close()
#s输出文件ini配置
conf.write(open("H:\\mission\\satelliateJiashan\\TEST2.ini","w"))