2015-01-15 53 views
2

我有用3.x版本编写的Python脚本,由于环境的限制,我需要在2.7中进行转换。Python 2.7 ConfigParser中的ExtendedInterpolation()

我设法重构大部分语法DIFS和模块导入,但我会在一行来到了,我不知道如何处理:

def readConfigFile(self, file): 
     config = ConfigParser.ConfigParser(interpolation = ConfigParser.ExtendedInterpolation()) 
     config.optionxform = str 
     config.read(file) 

**config = ConfigParser.ConfigParser(interpolation = ConfigParser.ExtendedInterpolation())** 

我发现了一些参考这里( Python ConfigParser interpolation from foreign section),但我不知道如何取代(重构)。

ConfigParser:

所以我的问题,是否有任何方法来重构上面的代码在Python 2.7和零售n功能?

回答

0

我得到这个用

from backports import configparser 

config = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation()) 
config.read('path/to/the/config_file.cfg') 

工作在ArcPy中2.7然后我在我的配置文件中使用下面的代码:

[Section 1] 
foo: bar 

...

[Section 5] 
foo: ${Section1:foo} 

尝试在解释器中的呼叫产生预测结果:

config['Section 5']['foo'] 
>>> bar