|
From: kat k. <dok...@gm...> - 2009-03-07 20:10:39
|
On Sat, Mar 7, 2009 at 5:41 AM, Michael Foord <fuz...@vo...>wrote:
> kat kat wrote:
> > Hello All,
> >
> > I need to retrieve a value of a key in the configuration file as a raw
> > string but somehow after ConfigObj reads it, it is escaping special
> > characters. For e.g, here is my configuration file:
> >
> > [ "constants" ]
> > 'target_dir' = r"C:\tmpdir"
> >
> > As you see above I have declared "target_dir" key's value to be a raw
> > string which should be evaluated literally, that is the backslash
> > present in the value should not be escaped.
>
> The raw string format is not valid ConfigObj syntax. This is what
> happens when I run your example:
>
> >>> open('cfg.txt', 'w').write("""[ "constants" ]
> ... 'target_dir' = r"C:\\tmpdir\"""")
> >>> from configobj import ConfigObj
> >>> c = ConfigObj('cfg.txt')
> >>> c['constants']['target_dir']
> 'r"C:\\tmpdir"'
> >>>
>
> See how the r is actually included in the value!
Yes. I have seen that.
>
>
> If we remove the leading r from the string then as far as I can tell you
> get what you are asking for anyway?
>
> >>> open('cfg.txt', 'w').write("""[ "constants" ]
> ... 'target_dir' = "C:\\tmpdir\"""")
> >>> c = ConfigObj('cfg.txt')
> >>> c['constants']['target_dir']
> 'C:\\tmpdir'
>
> If you are asking for this: 'C:\tmpdir' then you are simply confused
> about how Python handles strings - there isn't really two backslashes in
> the string, there is one but it has to be escaped to be displayed.
I understand that part. I know that 'C:\\tmpdir' has just one backslash,
although since this is not a raw string you need the other backslash to
escape the first one. See this:
>>> str_a = 'C:\\tmpdir'
>>> str_b = r'C:\tmpdir'
The both are essentially same. If you print the above two:
>>> print str_a
'C:\tmpdir'
>> print str_b
'C:\tmpdir'
As you see they both give the same output. Now see this:
>>> str_c = 'C:\tmpdir'
>> print str_c
C: mpdir
Which give the above and which is right because for python, it interprets \t
as a tab character, which is special character, because "str_c" is not
declared as a raw string. So I am not confused on that part.
What I want essentially is, if we have the following in some configuration
file:
[constants]
'target_dir': r'C:\tmpdir'
then "target_dir" should be interpreted as a raw string and the module that
is parsing this configuration file should not have a need of another
backslash to escape the first one because its raw and should be interpreted
literally. This works fine with ConfigParser though but my team ended up
using ConfigObj.
My point is this is a bug in ConfigObj if ConfigObj does not parse this
right.
>
> Michael
>
> >
> > I am reading the file like this:
> >
> > >>> from configobj import ConfigObj
> > >>> config = ConfigObj("tmp.cfg")
> > >>> dir = config['constants']['target_dir']
> > >>> dir
> > 'C:\\tmpdir'
> >
> > although this is OK:
> > >>> print dir
> > 'C:\tmpdir'
> >
> > but when we do print, the string is evaluated.
> >
> > If you see, the backslash gets escaped and I no longer get the raw
> > string I had in my configuration file.
> >
> > What should I do so that ConfigObj reads that value as a raw string
> > and give me r"C:\tmpdir" instead of "C:\\tmpdir"
> >
> > Thanks much for your help
> >
> > -kk
> >
> > ------------------------------------------------------------------------
> >
> >
> ------------------------------------------------------------------------------
> > Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco,
> CA
> > -OSBC tackles the biggest issue in open source: Open Sourcing the
> Enterprise
> > -Strategies to boost innovation and cut costs with open source
> participation
> > -Receive a $600 discount off the registration fee with the source code:
> SFAD
> > http://p.sf.net/sfu/XcvMzF8H
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Configobj-develop mailing list
> > Con...@li...
> > https://lists.sourceforge.net/lists/listinfo/configobj-develop
> >
>
>
> --
> http://www.ironpythoninaction.com/
> http://www.voidspace.org.uk/blog
>
>
>
>
> ------------------------------------------------------------------------------
> Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco,
> CA
> -OSBC tackles the biggest issue in open source: Open Sourcing the
> Enterprise
> -Strategies to boost innovation and cut costs with open source
> participation
> -Receive a $600 discount off the registration fee with the source code:
> SFAD
> http://p.sf.net/sfu/XcvMzF8H
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
|