On 6/18/11, Steven D'Aprano <st...@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. > > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor >
-- Have a great day, Alex (msg sent from GMail website) mehg...@gmail.com; http://www.facebook.com/mehgcap _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor