1 importconfigparser2 importos3
4 bakpostfix = '.ibk'
5
6 ## [修正不规则INI文件]
7 def __iniFileFix(errorline, iniFile, encoding):8 #备份INI文件名
9 newIniFile = iniFile +bakpostfix10
11 try:12 fr = open(iniFile, 'r', encoding=encoding)13 fw = open(newIniFile, 'w', encoding=encoding)14 #逐行读取
15 line =fr.readline()16 lineno = 1
17 while line != '':18 #若到了问题行,则注释它
19 if lineno ==int(errorline):20 line = ';' +line21 #逐行写入(修正后)
22 fw.writelines(line)23 line =fr.readline()24 lineno += 1
25 fr.close()26 fw.close()27 exceptException as e:28 #异常时返回报错信息
29 error = 'Error:[{0}]'.format(e)30 print(error)31 returnerror32 #正常时返回新文件名
33 returnnewIniFile34
35 ## [读取INI文件]
36 defgetINIValue(iniFile, section, option):37 config =configparser.ConfigParser()38 value = ''
39
40 #备份文件
41 bBakFile =False42
43 #异常:文件不存在
44 if (os.path.isfile(iniFile) ==False):45 print('Error: file "{0}" not exists...'.format(iniFile))46 returnNone47
48 bException =True49 #尝试编码的次数
50 counter = 1
51 #多次循环尝试修正INI为正确的格式,直到正常或遇到无法处理的异常
52 whilebException:53 try:54 encoding = ''
55 #依次进行如下编码打开尝试(后续根据需要添加)
56 if counter == 1: encoding = 'gb2312'
57 if counter == 2: encoding = 'utf-8'
58 if counter == 3: encoding = 'utf-16'
59 if counter == 4: encoding = 'gbk'
60 if counter == 5: encoding = 'gb18030'
61 if counter == 6:62 #暂无法处理的编码
63 print('Error: encoding unknown...')64 returnNone65 config.read(iniFile, encoding=encoding)66 bException =False67 exceptUnicodeDecodeError as e:68 #编码异常
69 print('Error:[{0}]'.format(e))70 counter += 1
71 except(configparser.DuplicateSectionError, configparser.DuplicateOptionError) as e:72 #SECTION重复异常
73 #Option重复异常
74 print('Error:[{0}]'.format(e))75 excep = '{0}'.format(e)76 errorline = excep[excep.find('[line') + len('[line'):excep.find(']', excep.find('[line'))]77 iniFile = __iniFileFix(errorline, iniFile, encoding)78 if iniFile.find('Error:[') ==0:79 returnNone80 bBakFile =True81 exceptconfigparser.MissingSectionHeaderError as e:82 #缺少SECTION头异常
83 print('Error:[{0}]'.format(e))84 excep = '{0}'.format(e)85 errorline = excep[excep.find(', line:') + len(', line:'):excep.find('\n', excep.find(', line:'))]86 iniFile = __iniFileFix(errorline, iniFile, encoding)87 if iniFile.find('Error:[') ==0:88 returnNone89 bBakFile =True90 exceptException as e:91 #未知新异常(后续根据需要追加)
92 print('Error:[{0}]'.format(e))93 returnNone94
95 #删除INI备份文件
96 if bBakFile ==True:97 #可能存在多个INI备份,循环删除
98 bMoreBakFile =True99 while(bMoreBakFile):100 os.remove(iniFile)101 iniFile =iniFile[0:iniFile.rfind(bakpostfix)]102 if iniFile.rfind(bakpostfix) == -1:103 bMoreBakFile =False104
105 #多次循环尝试修正INI为正确的格式,直到正常或遇到无法处理的异常
106 bException =True107 whilebException:108 try:109 value =config.get(section, option)110 bException =False111 exceptconfigparser.NoSectionError as e:112 #找不到SECTION
113 print('Error:[{0}]'.format(e))114 returnNone115 exceptconfigparser.NoOptionError as e:116 #找不到Option(考虑到一种情况:上位指定.reg的Option时没加",自动追加)
117 print('Error:[{0}]'.format(e))118 if option[0] != '"' and option[len(option)-1] != '"':119 option = '"' + option + '"'
120 else:121 returnNone122 exceptException as e:123 #未知新异常(后续根据需要追加)
124 print('Error:[{0}]'.format(e))125 returnNone126
127 return value