Python: Write dictionary to text file with ordered columns
Question: I have a dictionary D where:

    
    
    D = {'foo':{'meow':1.23,'mix':2.34}, 'bar':{'meow':4.56, 'mix':None}, 'baz':{'meow':None,'mix':None}}
    

I wrote this code to write it to a text file:

    
    
    def dict2txt(D, writefile, column1='-', delim='\t', width=20, order=['mix','meow']):
      import csv
      with open( writefile, 'w' ) as f:
        writer, w = csv.writer(f, delimiter=delim), []
        head = ['{!s:{}}'.format(column1,width)]
        for i in D[D.keys()[0]].keys(): head.append('{!s:{}}'.format(i,width))
        writer.writerow(head)
        for i in D.keys():
          row = ['{!s:{}}'.format(i,width)]
          for k in order: row.append('{!s:{}}'.format(D[i][k],width))
          writer.writerow(row)
    

But the output ignores `order = ['mix','meow']` and writes the file like:

    
    
    -       meow    mix    
    bar     None    4.56   
    foo     2.34    1.23456
    baz     None    None
    

How do I get it to write:

    
    
    -       mix     meow    
    bar     4.56    None   
    foo     1.23456 2.34
    baz     None    None
    

Thanks!

Update: Thanks to @SukritKalra in the comments below for pointing out that the
code works fine. I just wasn't reordering the column headers!

The line `for i in D[D.keys()[0]].keys():
head.append('{!s:{}}'.format(i,width))` should read `for i in order:
head.append('{!s:{}}'.format(i,width))`. Thanks folks!


Answer: Take advantage of your "order" variable to drive some generators:

    
    
    def dict2txt(D, writefile, column1='-', delim='\t', width=20, order=['mix','meow']):
        import csv
        # simplify formatting for clarity
        fmt = lambda s: '{!s:{}}'.format(s, width)
        with open(writefile, 'w') as f:
            writer = csv.writer(f, delimiter=delim)
            writer.writerow([fmt(column1)] + [fmt(s) for s in order])
            for i in D.keys():
                writer.writerow([fmt(i)] + [fmt(D[i][k]) for k in order])
    

The rows are still unordered. So you might want something like: "for i in
sorted(D.keys()):"

