0

I have a text file called sampl1.txt. This is what is inside this text file:-

111 112 113 114 115 

I have a .ini file called config_num.ini which contains:-

[num_group] file = sample1.txt 

Here is the code snippet:-

import ConfigParser config = ConfigParser.ConfigParser() config.read('config_num.ini') sample = config.get('num_group','file') print sample 

Is there any way to parse this so that when I read this 'file' and try to print it, it prints the elements which are in the txt file? Right now it prints sample1.txt. I want the numbers to printed.

2
  • does the content of config_num.ini stay the same? Commented Jul 1, 2014 at 7:34
  • yes the content stays the same in .ini file Commented Jul 1, 2014 at 7:37

2 Answers 2

3

You almost answered the question in itself!

import ConfigParser config = ConfigParser.ConfigParser() config.read('config_num.ini') sample = config.get('num_group','file') sample = open(sample, 'r').read() print sample 
Sign up to request clarification or add additional context in comments.

Comments

0

Well, you'd have to override your ConfigParser, but I'd advise only loading the file when you call get

from ConfigParser import ConfigParser class MyConfigParser(ConfigParser): def get(self, section, option, **kwargs): ret = super(MyConfigParser, self).get(section, option, **kwargs) if option == "file": try: return open(ret, 'r').read() except IOError: pass return ret 

Then you can create your new ConfigParser

cfg = MyConfigParser() cfg.read('config_num.ini') print cfg.get('num_group', 'file') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.