[Tutor] using configobj package to output quoted strings

Steven D'Aprano steve at pearwood.info
Sun Jun 19 04:29:08 CEST 2011


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?


> 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:

 >>> 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'


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.

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!



> 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!\""



-- 
Steven


More information about the Tutor mailing list