|
From: Michael F. <fuz...@vo...> - 2009-03-07 13:42:44
|
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!
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.
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
|