[ Problem configparser in python ]
Actually I am stuck in my work. I want to import a txt file into my python program which should have two lists of intergers.
The following program is working fine but I need to import the list 'a' and 'b' with the help of configparser.
It will be so nice if some one help me with it!
I am a begineer in python so please try to answer in an easy way...!
The program is as follow:
a=[5e6,6e6,7e6,8e6,8.5e6,9e6,9.5e6,10e6,11e6,12e6]
p=[0.0,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.01,0.015,0.05,0.1,0.15,0.2]
b=0
x=0
while b<=10:
c=a[b]
x=0
print '\there is the outer loop\n',c
while x<=15:
k=p[x]
print'here is the inner loop\n',k
x=x+1
b=b+1
Answer 1
Seems like ConfigParser is not the best tool for the job. You may implement the parsing logic youself something like:
a, b = [], []
with open('myfile', 'r') as f:
for num, line in enumerate(f.readlines()):
if num >= 10:
b.push(line)
else:
a.push(line)
or you can make up some other logic to devide the lists in your file. It depends on the way you want to represent it in you file
Answer 2
The json
module provides better support for lists in configuration files.
Instead of the ConfigParser (no list support) format, try using JSON
for this purpose.
JSON
(JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties makeJSON
an ideal data-interchange language.
Since your question smells like homework, I'll suggest an ugly hack. Use str.split()
and float()
to parse a list from a configuration file. Suppose the file x.conf
contains:
[sect1]
a=[5e6,6e6,7e6,8e6,8.5e6,9e6,9.5e6,10e6,11e6,12e6]
You can parse it with:
>>> import ConfigParser
>>> cf=ConfigParser.ConfigParser()
>>> cf.read(['x.conf'])
['x.conf']
>>> [float(s) for s in cf.get('sect1','a')[1:-1].split(',')]
[5000000.0, 6000000.0, 7000000.0, 8000000.0, 8500000.0, 9000000.0, 9500000.0, 10000000.0, 11000000.0, 12000000.0]
>>>
(The brackets around the list could be dropped from the configuration file, making the [1:-1]
hack unnecessary )
Answer 3
Yea, the config parser probably isn't the best choice...but if you really want to, try this:
import unittest
from ConfigParser import SafeConfigParser
from cStringIO import StringIO
def _parse_float_list(string_value):
return [float(v.strip()) for v in string_value.split(',')]
def _generate_float_list(float_values):
return ','.join(str(value) for value in float_values)
def get_float_list(parser, section, option):
string_value = parser.get(section, option)
return _parse_float_list(string_value)
def set_float_list(parser, section, option, float_values):
string_value = _generate_float_list(float_values)
parser.set(section, option, string_value)
class TestConfigParser(unittest.TestCase):
def setUp(self):
self.a = [5e6,6e6,7e6,8e6,8.5e6,9e6,9.5e6,10e6,11e6,12e6]
self.p = [0.0,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.01,0.015,0.05,0.1,0.15,0.2]
def testRead(self):
parser = SafeConfigParser()
f = StringIO('''[values]
a: 5e6, 6e6, 7e6, 8e6,
8.5e6, 9e6, 9.5e6, 10e6,
11e6, 12e6
p: 0.0 , 0.001, 0.002,
0.003, 0.004, 0.005,
0.006, 0.007, 0.008,
0.009, 0.01 , 0.015,
0.05 , 0.1 , 0.15 ,
0.2
''')
parser.readfp(f)
self.assertEquals(self.a, get_float_list(parser, 'values', 'a'))
self.assertEquals(self.p, get_float_list(parser, 'values', 'p'))
def testRoundTrip(self):
parser = SafeConfigParser()
parser.add_section('values')
set_float_list(parser, 'values', 'a', self.a)
set_float_list(parser, 'values', 'p', self.p)
self.assertEquals(self.a, get_float_list(parser, 'values', 'a'))
self.assertEquals(self.p, get_float_list(parser, 'values', 'p'))
if __name__ == '__main__':
unittest.main()