2013-10-15 81 views
0

當我在Linux中使用python解析它時,如何將輸入作爲逗號分隔值輸入ini文件中。ini配置文件中的逗號分隔值

下面你會發現MWE

#!/usr/bin/env python 

import configparser 

def parse_config(file): 
     global config_dictionary 
     config_dictionary={} 
     config=configparser.ConfigParser() 
     config.read(file) 
     for section in config.sections(): 
       config_dictionary[section] = {} 
       for option in config.options(section): 
         config_dictionary[section][option] = config.get(section, option) 
     return config_dictionary 

def main(): 
     config_path = 'test.ini' 
     config_dict=parse_config(config_path) 
     mapping = config_dict['Options']['initialassignment'] 
     mapping = list(mapping) 
     print mapping 

if __name__ == "__main__": 
     main() 

test.ini文件

[Options] 
initialAssignment=0,1,2,3 

回答

0
mapping = config_dict['Options']['initialassignment'] 
mapping = list(mapping) 
mapping = [int(x) for x in mapping if x != ","] 
print mapping 

這肯定是不乾淨的方式。但它的工作原理。任何建議都會有所幫助!由於

0

使用引號您的ini文件:

[選項]

initialAssignment = 「0,1,2,3」

時將禁止 '' 作爲字段分隔符。