Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-09 Thread Peter Otten
Kayode Odeyemi wrote:

> Thanks so much. This is exactly what I'm looking for. In addition, since
> fields is obviously a dict, I won't want to have to display it's keys
> repeatedly. Is there a way to get the keys once, have it displayed and
> used as columns, then it's values are displayed beneath it. Something
> like:
> 
> updated tel
> 2011 3456
> 2011 34510
> 
> right now, I'm having it displayed as
> 
> updated
> 2011
> tel
> 3456
> updated
> 2011
> tel
> 34510
> 
> I am having the fields being written using a writer like this:
> 
> x = {'pk': 1L, 'model': 'trans', 'fields': {'updated': 2011, 'tel':
> 3456}},
> {'pk': 2L, 'model': 'trans2', 'fields': {'updated': 2011, 'tel': 34510}}
> writer = csv.writer(response)
> 
> for entry in x:
>for key, value in entry.items():
>print key
>if key == "fields":
>for field_key, field_value in value.items():
>d = [field_key] #a list of sequence
>x = [field_value]
>writer.writerow(d)
>writer.writerow(x)
>else:
>print "  %s" % value

You are doing too much at once. You are only interested in the "fields" 
dictionary, so I recommend that you extract that first:

items = [
{'pk': 1, 'model': 'trans', 'fields': {'updated': 2011, 'tel': 3456}},
{'pk': 2, 'model': 'trans2', 'fields': {'updated': 2011, 'tel': 34510}}
]

rows = [item["fields"] for item in items]

Now the data structure has become simpler, just a list of dictionaries, you 
can think about how you generate the output. One way, the str.format() 
method, was already mentioned:

>>> for row in rows:
... print "{updated}\t{tel}".format(**row)
...
20113456
201134510

If you want to continue to use the csv module you should use the DictWriter:

>>> import csv, sys
>>> fieldnames = rows[0].keys()
>>> writer = csv.DictWriter(sys.stdout, fieldnames)
>>> writer.writeheader()
updated,tel
>>> writer.writerows(rows)
2011,3456
2011,34510


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-08 Thread Prasad, Ramit
>I would suggest using the string format() method to lay out the columns 
>as you want them.  You could use tab, but if your data values vary much, 
>you'll get inconsistent results (i.e., if one row's value goes past the 
>next tab stop).  You'll want to create your own for loop to iterate over 
>the (possibly sorted) list of values, printing each row the way you want it.

+1

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-08 Thread Steve Willoughby

On 08-Aug-11 02:20, Kayode Odeyemi wrote:

Either way, once you have the list of the keys you want to display,
print them out once as column headings


My concern is how do I print them out as columns, since writer.writerows
is currently lining them out as rows (\n)
Is there a function available for me to do this or do I have to
construct the column structure myself, possibly leveraging
the tab character or just basically construct a while loop and append
spaces to each keys found?


I would suggest using the string format() method to lay out the columns 
as you want them.  You could use tab, but if your data values vary much, 
you'll get inconsistent results (i.e., if one row's value goes past the 
next tab stop).  You'll want to create your own for loop to iterate over 
the (possibly sorted) list of values, printing each row the way you want it.




Thanks

On Mon, Aug 8, 2011 at 9:41 AM, Steve Willoughby mailto:st...@alchemy.com>> wrote:

Either way, once you have the list of the keys you want to display,
print them out once as column headings




--
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde




--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-08 Thread Kayode Odeyemi
>
> Either way, once you have the list of the keys you want to display, print
> them out once as column headings


My concern is how do I print them out as columns, since writer.writerows is
currently lining them out as rows (\n)
Is there a function available for me to do this or do I have to construct
the column structure myself, possibly leveraging
the tab character or just basically construct a while loop and append spaces
to each keys found?

Thanks

On Mon, Aug 8, 2011 at 9:41 AM, Steve Willoughby  wrote:

> Either way, once you have the list of the keys you want to display, print
> them out once as column headings




-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-08 Thread Steve Willoughby

On 08-Aug-11 01:18, Kayode Odeyemi wrote:

Thanks so much. This is exactly what I'm looking for. In addition, since
fields is obviously a dict, I won't want to have to display it's keys
repeatedly. Is there a way to get the keys once, have it displayed and
used as columns, then it's values are displayed beneath it. Something like:

updated tel
20113456
201134510


Sure.  You have to make the assumption that all the dictionaries in your 
list x have the same keys, or iterate over all of them, making a master 
list of all the keys you found.  Either way, once you have the list of 
the keys you want to display, print them out once as column headings, 
then iterate over the list, printing each key's value in the same order 
each time.


Another approach, if it seems like you're stuffing a lot of data into 
complex dictionary structures, is to turn this into a class.  That way 
you can define the attributes of each object and even implement a 
print() method (or at least define __str__() and/or __repr__() methods) 
to print each object sensibly.




--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-08 Thread Kayode Odeyemi
Thanks so much. This is exactly what I'm looking for. In addition, since
fields is obviously a dict, I won't want to have to display it's keys
repeatedly. Is there a way to get the keys once, have it displayed and used
as columns, then it's values are displayed beneath it. Something like:

updated tel
2011 3456
2011 34510

right now, I'm having it displayed as

updated
2011
tel
3456
updated
2011
tel
34510

I am having the fields being written using a writer like this:

x = {'pk': 1L, 'model': 'trans', 'fields': {'updated': 2011, 'tel': 3456}},
{'pk': 2L, 'model': 'trans2', 'fields': {'updated': 2011, 'tel': 34510}}
writer = csv.writer(response)

for entry in x:
   for key, value in entry.items():
   print key
   if key == "fields":
   for field_key, field_value in value.items():
   d = [field_key] #a list of sequence
   x = [field_value]
   writer.writerow(d)
   writer.writerow(x)
   else:
   print "  %s" % value


On Sun, Aug 7, 2011 at 5:45 PM, Steve Willoughby  wrote:

> On 07-Aug-11 09:17, Steve Willoughby wrote:
>
>  First of all, that's not a dict. It's just a tuple of strings.
>> so, when you say:
>>
>>  #loop through and get the keys of each
>>> for k,v in x:
>>>
>>
>> You'll get one iteration, where k=the first string and v=the second.
>> However, you ignore k and v in all the code that follows, so I'm really
>> confused what you're trying to do here.
>>
>
> I just noticed I glossed over a key point, for that to work you'd need x to
> be a sequence of such tuples.  As it stands there, you'll get an error
> because each element of x is a single string, so it can't pull two values
> out of each of them.
>
>
> --
> Steve Willoughby / st...@alchemy.com
> "A ship in harbor is safe, but that is not what ships are built for."
> PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-07 Thread Steve Willoughby

On 07-Aug-11 09:17, Steve Willoughby wrote:


First of all, that's not a dict. It's just a tuple of strings.
so, when you say:


#loop through and get the keys of each
for k,v in x:


You'll get one iteration, where k=the first string and v=the second.
However, you ignore k and v in all the code that follows, so I'm really
confused what you're trying to do here.


I just noticed I glossed over a key point, for that to work you'd need x 
to be a sequence of such tuples.  As it stands there, you'll get an 
error because each element of x is a single string, so it can't pull two 
values out of each of them.


--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-07 Thread Knacktus

Am 07.08.2011 17:37, schrieb Kayode Odeyemi:

Hello all,

Please I need help figuring out this permutation.

I have a dict like this:

x = "{'pk': 1L, 'model': 'trans', 'fields': {'updated': 2011, 'tel':
3456}", "{'pk': 2L, 'model': 'trans2', 'fields': {'updated': 2011,
'tel': 34510}";
This is not a dict, it's a tuple of two strings. (By the way, the ";" is 
only needed if you would want to place several statements in one line, 
which you shouldn't do.)


A tuple of dicts would be:

x = {'pk': 1L, 'model': 'trans', 'fields': {'updated': 2011, 'tel': 
3456}}, {'pk': 2L, 'model': 'trans2', 'fields': {'updated': 2011, 'tel': 
34510}}


Note the additional closing "}" for each entry. You can loop over the 
entries of your tuple and check the type:


for entry in x:
print type(entry)

Now you can extend this loop and loop over the keys and values for each 
entry:


for entry in x:
for key, value in entry.items():
print key
print value

If key is "fields" then value is another dictionary. So if you wish you 
could also get the entries of those dicts explicitly:


for entry in x:
for key, value in entry.items():
print key
if key == "fields":
for field_key, field_value in value.items():
print "%s" % field_key
print "  %s" % field_value
else:
print "  %s" % value

For string formatting used here see:

http://docs.python.org/library/stdtypes.html#string-formatting-operations

HTH,

Jan



#loop through and get the keys of each
for k,v in x:
 keys = dict(x).keys()
print keys

I get ValueError: too many values to unpack

Any help will be much appreciated.

Regards


--
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-07 Thread Steve Willoughby

On 07-Aug-11 08:37, Kayode Odeyemi wrote:

Hello all,

Please I need help figuring out this permutation.

I have a dict like this:

x = "{'pk': 1L, 'model': 'trans', 'fields': {'updated': 2011, 'tel':
3456}", "{'pk': 2L, 'model': 'trans2', 'fields': {'updated': 2011,
'tel': 34510}";


First of all, that's not a dict.  It's just a tuple of strings.
so, when you say:


#loop through and get the keys of each
for k,v in x:


You'll get one iteration, where k=the first string and v=the second.
However, you ignore k and v in all the code that follows, so I'm really 
confused what you're trying to do here.



 keys = dict(x).keys()


Now you try to create a dict out of a tuple of strings, which won't 
quite work as written but in principle would only have created a 
dictionary with the first string as the key, and the second as value, 
not nested dictionaries.


The dict() constructor really expects to see a sequence of key/value 
pairs, so it's going to take your sequence x as a list of two such 
pairs, and then raise an exception because the strings are not key/value 
pairs.  This would succeed if you said

  keys = dict((x,)).keys()
perhaps, but it's still not at all what you're trying to accomplish.


print keys


Note that you are re-assigning the value of keys each time through the 
loop but printing its last value after the loop exits.  Is that what you 
intended?



You're expecting Python to take a string that looks like Python source 
code and know that you want it to be interpreted as source code. 
Instead of using string values, use actual Python syntax directly, like:


 x = {
'pk': 1L,
'model': 'trans',
'fields': {
'updated': 2011,
'tel': 3456
}
}


--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-07 Thread Kayode Odeyemi
Hello all,

Please I need help figuring out this permutation.

I have a dict like this:

x = "{'pk': 1L, 'model': 'trans', 'fields': {'updated': 2011, 'tel':
3456}", "{'pk': 2L, 'model': 'trans2', 'fields': {'updated': 2011, 'tel':
34510}";

#loop through and get the keys of each
for k,v in x:
keys = dict(x).keys()
print keys

I get ValueError: too many values to unpack

Any help will be much appreciated.

Regards


-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor