timc3 wrote:
> I am having a problem with getting lists/dictionaries out of the
> database.
>
> If I have for instance this in a field of my database:
>
> [['Image format', 'JPEG'], ['Image mode', 'RGB'], ['Image size',
> '1440x900'], ['Compression', '21.0 times']]
>
This is a single field? That's unfortunate.
> And I call it from django, I would get the following string:
Be careful: this isn't a string, it's a list with a single dictionary in
it (but that's really just picking nits). By "call it" I assume you mean
"retrieve it". I know it's tedious of me to correct you on these terms,
but you need to understand that you are dealing with a bunch of geeks
who ascribe precise technical meaning to terms like "call" ... anyway,
enough with the linguistics. And sorry if English isn't your first language.
>
> [{'metadata': u"[['Image format', 'JPEG'], ['Image mode', 'RGB'],
> ['Image size', '1280x960'], ['Compression', '14.0 times']]", 'title':
> u'Technical Metadata'}]
>
> Because of the 'u' UTF it seems that I can't then properly use it as a
> list with dictionaries like so:
>
The u stands for "Unicode", not "UTF". But you will note that "metadata"
isn't a Unicode string, it's a regular ASCII string, so Unicode doesn't
come into that.

You seem to have the basics correct, and

myProblemObject[0]

should indeed be a dictionary. Therefore

myProblemObject[0]['metadata']

will be the (Unicode) string (ignoring any wrapping the mail system does)

u"[['Image format', 'JPEG'], ['Image mode', 'RGB'], ['Image size',
'1280x960'], ['Compression', '14.0 times']]"

This means that
> myProblemObject[0]['metadata'][0]
will give you a single character, u"[", the first character from that
string.
>
> Any ideas? I presume this is Django's handling of the data within the
> database being the problem not a python problem in general?
I prefer to suggest that the way you are *storing* the data is a
problem, since it would really be better if your model had separate
fields for image format, image mode, image size (possibly separating
width and heigh) and compression. Otherwise youo will forever be
fighting to extract meaningful information form the data stored on the
database.

If you want a really dirty solution you could try

metadata = eval(myProblemObject[0]['metadata'])

which will give you a list of two-element lists, but this is very
fragile and extremely unsafe if anyone else can add to the database
contents.

regards
 Steve


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to