File processing

G

Gopal

Hello,

I'm Gopal. I'm looking for a solution to the following problem:

I need to create a text file config.txt having some parameters. I'm
thinking of going with this format by having "Param Name - value". Note
that the value is a string/number; something like this:

PROJECT_ID = "E4208506"
SW_VERSION = "18d"
HW_VERSION = "2"

In my script, I need to parse this config file and extract the Values
of the parameters.

I'm very new to python as you can understand from the problem. However,
I've some project dealines. So I need your help in arriving at a simple
and ready-made solution.

Regards,
Gopal.
 
J

Jeremy Jones

Gopal said:
Hello,

I'm Gopal. I'm looking for a solution to the following problem:

I need to create a text file config.txt having some parameters. I'm
thinking of going with this format by having "Param Name - value". Note
that the value is a string/number; something like this:

PROJECT_ID = "E4208506"
SW_VERSION = "18d"
HW_VERSION = "2"

In my script, I need to parse this config file and extract the Values
of the parameters.

I'm very new to python as you can understand from the problem. However,
I've some project dealines. So I need your help in arriving at a simple
and ready-made solution.

Regards,
Gopal.
Would this
(http://www.python.org/doc/current/lib/module-ConfigParser.html) do what
you need? It's part of the standard library.


- JMJ
 
G

Gopal

Thanks for the reference. However, I'm not understanding how to use it.
Could you please provide with an example? Like I open the file, read
line and give it to parser?

Please help me.
 
P

Peter Hansen

Gopal said:
Hello,

I'm Gopal. I'm looking for a solution to the following problem:

I need to create a text file config.txt having some parameters. I'm
thinking of going with this format by having "Param Name - value". Note
that the value is a string/number; something like this:

PROJECT_ID = "E4208506"
SW_VERSION = "18d"
HW_VERSION = "2"

In my script, I need to parse this config file and extract the Values
of the parameters.

I'm very new to python as you can understand from the problem. However,
I've some project dealines. So I need your help in arriving at a simple
and ready-made solution.

Luckily, you're already done! Just make sure the above file has a .py
extension, say maybe "config.py", and then in code where you need to
"parse" it and extract the values you can just do this:

import config
print 'Project Id is', config.PROJECT_ID

-Peter
 
J

Jeremy Jones

Gopal said:
Thanks for the reference. However, I'm not understanding how to use it.
Could you please provide with an example? Like I open the file, read
line and give it to parser?

Please help me.
I had thought of recommending what Peter Hansen recommended - just
importing the text you have as a Python module. I don't know why I
recommended ConfigParser over that option. However, if you don't like
what Peter said and would still like to look at ConfigParser, here is a
very simple example. Here is the config file I created from your email:

jmjones@qiwi 8:36AM configparser % cat foo.txt
[main]
PROJECT_ID = "E4208506"
SW_VERSION = "18d"
HW_VERSION = "2"


Here is me running ConfigParser from a Python shell:

In [1]: import ConfigParser

In [2]: p = ConfigParser.ConfigParser()

In [3]: p.read("foo.txt")
Out[3]: ['foo.txt']

In [4]: p.get("main", "PROJECT_ID")
Out[4]: '"E4208506"'


Note that the value of ("main", "PROJECT_ID") is a string which contains
double quotes in it. If you take Peter's advice, you won't have that
problem; the config file will preserve your types for you.

HTH,

- JMJ
 
B

Bryan Olson

Peter said:
>> [...] I'm
>> thinking of going with this format by having "Param Name - value". Note
>> that the value is a string/number; something like this:
>>
>> PROJECT_ID = "E4208506"
>> SW_VERSION = "18d"
>> HW_VERSION = "2"
>>
>> In my script, I need to parse this config file and extract the Values
>> of the parameters.
> Luckily, you're already done! Just make sure the above file has a .py
> extension, say maybe "config.py", and then in code where you need to
> "parse" it and extract the values you can just do this:
>
> import config
> print 'Project Id is', config.PROJECT_ID

In many cases that's fine, but at least be aware that the party
supplying the config data also gets the ability to change
the behavior of the process in ways of his choosing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top