writing dictionaries to a file

Josh Bloom joshbloom at gmail.com
Wed Mar 21 15:38:15 EDT 2007


Where have you defined the relationship between the 2 dictionaries?

If you know that all the someys are related to the somex you can do
something like this:

somex = {'unit':'00', 'type':'processing'}

somey1 = {'comment':'fair', 'code':'aaa'}
somey2 = {'comment':'fair', 'code':'bbb'}
somey3 = {'comment':'fair', 'code':'ccc'}

# Put all the dicts in a List
dictList = []
dictList.append(somey1)
dictList.append(somey2)
dictList.append(somey3)

for dict in dictList: #Iterate over the lists
    print ("%s, %s") % (somex['unit'], dict['code']) #Print just the info
that you want

I'll leave writing it to a file as an exercise in Googling.
-Josh


On 3/21/07, kavitha thankaian <kavithapython at yahoo.co.in> wrote:
>
> Hi All,
>
> I have two dictionaries:
>
> somex:{'unit':00, type:'processing'}
>
> somey:{'comment':'fair', 'code':'aaa'}
> somey:{'comment':'good', 'code':bbb'}
> somey:{'comment':'excellent', 'code':'ccc'}
>
>
> now i would like to write this to a file in the following format(unit,
> code),,,the output should be as follows written to a file,,,
>
>           00, aaa
>           00, bbb
>           00, ccc
>
> can someone help me?
>
> Regards
> Kavitha
>
>
> ------------------------------
> Here's a new way to find what you're looking for - Yahoo! Answers<http://us.rd.yahoo.com/mail/in/yanswers/*http://in.answers.yahoo.com/>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070321/e471511f/attachment.html>


More information about the Python-list mailing list