[Tutor] using configobj package to output quoted strings

Dave Angel davea at ieee.org
Sun Jun 19 13:33:55 CEST 2011


On 01/-10/-28163 02:59 PM, Alex Hall wrote:
> On 6/18/11, Steven D'Aprano<steve at pearwood.info>  wrote:
>> Alex Hall wrote:
>>> Hello all,
>>> I am using the configobj package to handle a ridiculously simple ini
>>
>> What's configobj?
>>
>>   >>>  import configobj
>> Traceback (most recent call last):
>>     File "<stdin>", line 1, in<module>
>> ImportError: No module named configobj
>>
>> It's not in the 3.1 standard library. Is it a third-part package, or one
>> you have written yourself, or something else?
> Right, sorry. http://www.voidspace.org.uk/python/configobj.html
>>
>>
>>> file, which takes the form:
>>> [favorites]
>>> "search name" = "search terms", "search type"
>>>
>>> for any number of searches stored in the favorites section. I need the
>>> search type, term, and name to be read in as strings, but they will
>>> usually contain spaces, which is why I put them in quotes.
>>
>> You shouldn't need quotes. The standard library configparser module
>> doesn't need them:
> I'll have a look at it, but I liked configobj's dictionary approach to
> everything.
>>
>>   >>>  import configparser
>>   >>>  ini = configparser.ConfigParser(
>> ...       {"key with spaces": "value with spaces, and a comma"})
>>   >>>  ini.defaults()
>> OrderedDict([('key with spaces', 'value with spaces, and a comma')])
>>   >>>
>>   >>>  ini.get("DEFAULT", "key with spaces")
>> 'value with spaces, and a comma'
> The value is supposed to be a list; in python, you might write a
> search like this:
> searches={
>   "my first search":["the search terms", "the search type"]
> }
>>
>>
>> You can use quotes if you like, but why bother?
>>
>>
>>   >>>  ini.set('DEFAULT', '"search name"', "some value")
>>   >>>  ini.defaults()
>> OrderedDict([('key with spaces', 'value with spaces, and a comma'),
>> ('"search name"', 'some value')])
>>
>>
>>
>>> Here is the
>>> question: when I try to add a search to the ini file, I cannot get the
>>> quotes to show up correctly. I just want my three strings enclosed in
>>> single or double quotes, but I cannot manage to do it. I either get no
>>
>> What do you mean by "show up"? What are you using to do the showing?
>>
>> Remember, that when Python displays strings, the outer-most set of
>> quotes are not part of the string. They're just the syntax to tell
>> Python you are typing a string, and not part of the string, in the same
>> way that the [ ] surrounding a list are not actually part of the list.
> I know, that is why I was using backslashes to escape quotes, trying
> to force the quotes to be outputted to the file when I call
> configobj's write() method, which takes the entire configuration and
> puts it in a .ini file.
>>
>> So if I do this:
>>
>>   >>>  s = "hello world"
>>   >>>  s
>> 'hello world'
>>
>> The string is made up of characters h e l l o etc. and *not* the quotes.
>> You can see the string without the delimiters by using print:
>>
>>   >>>  print(s)
>> hello world
>>
>> but if you print an object containing a string, you still get the quotes
>> (for obvious reasons):
>>
>>   >>>  print([1, 2, s])
>> [1, 2, 'hello world']
>>
>>
>> So, the question is, are you mistaking the string syntax for the
>> contents of the string? If so, you will hardly be the first one!
> No, the configobj will not see the multiple words of the string as a
> single value unless there are quotes in the config file.
>>
>>
>>
>>> quotes, or, when I manually add them (such as "\""+searchTerms+"\""
>>> and so on), I get strings surrounded by both double and single quotes.
>>
>> Sounds like you are mistaking them.
>>
>> If you do this:
>>
>>   >>>  searchTerms = "hello world"
>>   >>>  "\"" + searchTerms + "\""
>> '"hello world"'
>>
>> The single quotes are just delimiters. The double quotes are part of the
>> string, exactly as you wanted.
>>
>> Also, there is never any need to write something as ugly as "\"". This
>> is why Python gives you a choice of two different string delimiters,
>> single and double quotes. That is better written as:
>>
>> '"'
>>
>> with no need to escape the inner quote. The only good reason to escape a
>> quotation mark is if you need *both* quote marks in a single string:
>>
>> "John Cleese said, \"'e's not dead, 'e's just pining for the fjords!\""
> Good point. Still, it is odd (well, to me at least) that when I write
> the string to the file with no quotes, I get no quotes, but using
> double quotes in the string's value gives me both single and double
> quotes.
>>
>>


When a string is interpreted with repr(), if it has any embedded quotes 
in it, it will be displayed with the others around the outside.  But if 
it has no funny characters, it will still put quotes around it.  str(), 
on the other hand, doesn't add either one.

Without any context, we've been guessing wildly.  it's good that you now 
include a link to the package. But you still aren't showing what calls 
you use to write to the file, nor what calls you're using to read the 
file.  By now, you should be able to write a trivial program to 
demonstrate the problem.  Show the code, and show the console output 
when you run that code.  Plus show the content of the .ini file.

I figure there are two ways you could be writing the file, one from 
within this configobj package, and the other with a text editor.  And 
there's two ways you could be reading the file, with a text editor and 
with this configobj package.  Be explicit, especially because apparently 
none of us are familiar with the package.

DaveA


More information about the Tutor mailing list