Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #4960
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; '(at': 0.03; 'string,': 0.05; 'dictionary': 0.07; 'function,': 0.07; 'python': 0.07; "(it's": 0.09; '>>>>': 0.09; 'hash': 0.09; 'it;': 0.09; 'okay': 0.09; 'parsing': 0.09; 'postgres': 0.09; 'time)': 0.09; 'valueerror:': 0.09; 'this:': 0.11; 'am,': 0.14; 'wrote:': 0.14; 'lindstrom': 0.16; 'non-printing': 0.16; 'traceback': 0.16; '(most': 0.16; 'header:In-Reply-To:1': 0.22; '(but': 0.22; 'convert': 0.22; 'mon,': 0.22; 'values': 0.23; 'last):': 0.23; 'parse': 0.23; 'received:209.85.214.174': 0.23; 'received:mail- iw0-f174.google.com': 0.23; 'appears': 0.24; 'easiest': 0.25; 'greg': 0.25; 'value.': 0.25; "i'm": 0.26; '(in': 0.27; 'chris': 0.27; 'message-id:@mail.gmail.com': 0.28; 'string': 0.29; 'subject:?': 0.29; 'assuming': 0.29; 'useless': 0.31; 'does': 0.31; 'actual': 0.31; 'to:addr:python-list': 0.32; 'done': 0.32; 'character': 0.33; 'lines': 0.34; 'there': 0.35; 'file': 0.35; '"",': 0.35; 'appreciated.': 0.36; 'received:209.85': 0.37; 'element': 0.38; 'sequence': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'skip:" 20': 0.38; 'database': 0.38; 'used': 0.38; 'here,': 0.39; 'help': 0.39; 'received:209.85.214': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'received:209': 0.39; 'solution': 0.40; 'would': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'database,': 0.60; '2011': 0.62; 'guarantee': 0.75; 'safe.': 0.95 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=tzX4qbysRoKL+jAKVDtCHYsSgV4xSfIdB/ZKv906uBQ=; b=QObgt0HRxA/PDAa1JSSciLpjcZViPIj+CZITEd+qffLcw7SLj/CvAY6M7qiApRjoqR gcZ+DwEjUXobq01IAYwD7lz5iWQlhqf9dATzilHtsUWEFDdjxXc4k+Tu146/pSHeDaoN eewe+TWF3WmfDDuZlkwnApQbXFJJUTmU+ml6Y= |
| DomainKey-Signature | a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=Xbb7Nm1XudUyfbm/XyzEfd1SEQur26zZlGOvISpQreA7PsaX1X8aqX3Nn8zXigVUvJ YKf1/dOGz+5uybexx7Nm9UGkzp4aeW2/nYPcSTQG0MnApp2eOdHDs4Bm+buHGJS4GubZ SxxFSIfXdym6Mby0ks1LrWxhvw+wSd6dbq8jk= |
| MIME-Version | 1.0 |
| In-Reply-To | <BANLkTi=Gkp3c_5oLnnW8n-UWQ5T+cNcFZw@mail.gmail.com> |
| References | <BANLkTi=Gkp3c_5oLnnW8n-UWQ5T+cNcFZw@mail.gmail.com> |
| Date | Mon, 9 May 2011 02:16:50 +1000 |
| Subject | Re: Dictionary from String? |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1310.1304871414.9059.python-list@python.org> (permalink) |
| Lines | 50 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1304871414 news.xs4all.nl 81474 [::ffff:82.94.164.166]:48722 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:4960 |
Show key headers only | View raw
On Mon, May 9, 2011 at 1:20 AM, Greg Lindstrom <gslindstrom@gmail.com> wrote:
> Is it possible to create a dictionary from a string value? Something along
> these lines (but that works):
>
>>>> mystring = "{'name':'greg','hatsize':'7 5/8'}"
>>>> mystring
> "{'name':'greg','hatsize':'7 5/8'}"
>>>> dict(mystring)
> Traceback (most recent call last):
> File "<string>", line 1, in <fragment>
> ValueError: dictionary update sequence element #0 has length 1; 2 is
> required
>>>>
>
> I would like to return an undetermined (at call time) number of fields from
> a postgres database (only 1 record) for a given request. My thought is that
> I could build a dictionary in the form of a string, return the string and
> then convert the string value to a dictionary. I can do that now, but I
> have to parse the string and then build the dictionary. Any thoughts or
> help you could provide would be appreciated.
The first one can be done with the eval() function, but you want to be
REALLY sure that the string is safe. But if you're building the
dictionary from a database, it would be far more efficient to build it
directly in Python and return it; I'm assuming that you're currently
building the string outside of Python, but the easiest solution (in my
opinion, based solely on the information given and potentially
hopelessly useless to your actual situation) would be to simply return
multiple values from postgres and have Python do the parsing and
building.
Alternatively, if there is a character that you can guarantee does not
exist in the content, you could do something like this:
mystring = "name=greg#hatsize=7 5/8"
dict((field.split('=',1) for field in mystring.split('#')))
I used the hash character as a separator here, but you could just as
easily use a non-printing character like \n if you can be sure it
never appears in any string value. (It's okay for an equals sign, as
long as it's not in the field name.)
Hope that's of value!
Chris Angelico
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Dictionary from String? Chris Angelico <rosuav@gmail.com> - 2011-05-09 02:16 +1000
csiph-web