2015-01-15 109 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