How to write the data of 3 dictionaries in a table separated by tab into a text file?
Question: Say I have the following 3 dictionaries:

    
    
    d1 = {'Ben': {'Skill': 'true', 'Magic': 'false'}, 'Tom': {'Skill': 'true', 'Magic': 'true'}}
    d2 = {'Ben': {'Strength': 'wo_mana', 'Int': 'wi_mana', 'Speed': 'wo_mana'}, 'Tom': {'Int': 'wi_mana', 'Agility': 'wo_mana'}}
    d3 = {'Ben': {'Strength': '1.10', 'Int': '1.20', 'Speed': '1.50'}, 'Tom': {'Int': '1.40', 'Agility': '1.60'}}
    

I want to write the data of the 3 dictionaries above into a table separated by
tab into a .txt or .csv file using `with open('filename', 'w') as f:`

My desired output (when opened in Excel):

    
    
    Name Skill Magic wo_mana          wi_mana
    Ben  true  false Strength = 1.10  Int = 1.20
                     Speed = 1.50
    Tom  true  true  Agility = 1.60   Int = 1.40
    

My code so far:

    
    
    with open('output.txt', 'w')as f:
      f.write("Name\tSkill\tMagic\two_mana\twi_mana\n")
      for key in d1:
        f.write('%s\t%s\t%s\n' %(key, d1[key]['Skill'], d1[key]['Magic']))
    

and I got this:

    
    
    Name Skill Magic wo_mana wi_mana
    Ben  true  false
    Tom  true  true
    

How am I supposed to write the `wo_mana` and `wi_mana` part without using the
`xlsxwriter` module?

Note:

a) The 3 dictionaries are created when extracting the data from a input file,
the keys and values are not defined by myself, hence I do not know the order
of the keys and values in the dictionaries.

b) I wish to write into a .txt or .csv file which will be opened in Excel with
tab as the delimiter.

c) I am using Python 2.7.


Answer: I was not able to produce your exact desired output, but I got something that
will work in Excel.

Your three dictionaries each contain information about an object (or
character) so I created a character class rather than use your dictionaries.

    
    
    class Character(object):
        def __init__(self, name, skill, magic, skill_list):
            """
            Initialize the character.  Skill and magic are boolean.
            Skill list is a list of skill tuples. A skill tuple has
            the format: (skill, value, mana)
            """
    
            self.name = name
            self.skill = skill
            self.magic = magic
            self.skills = {s[0]: (s[1], s[2]) for s in skill_list}
    

Then create each character:

    
    
    ben_skills = [
        ('Strength', 1.10, 'wo_mana'),
        ('Speed', 1.50, 'wo_mana'),
        ('Int', 1.20, 'wi_mana')
    ]
    tom_skills = [
        ('Agility', 1.60, 'wo_mana'),
        ('Int', 1.40, 'wi_mana')
    ]
    characters = [
        Character('Ben', True, False, ben_skills),
        Character('Tom', True, True, tom_skills)
    ]
    

And write them to a CSV file (Excel knows how to read these):

    
    
    with open('output.csv', 'w') as f:
        f.write('Name,Skill,Magic,wo_mana,wi_mana\n')
    
        for c in characters:
            wo_mana = []
            wi_mana = []
    
            for k, s in c.skills.items():
                if s[1] == 'wo_mana':
                    wo_mana.append('{} = {}'.format(k, s[0]))
                elif s[1] == 'wi_mana':
                    wi_mana.append('{} = {}'.format(k, s[0]))
    
            f.write('{},{},{},{},{}\n'.format(
                c.name,
                str(c.skill),
                str(c.magic),
                '; '.join(wo_mana),
                '; '.join(wi_mana)
            ))
    

There is probably a better way to do this. Using `import csv` could have
improvements (read about CSV
[here](https://docs.python.org/2/library/csv.html)).

Hope this helps!

