简体   繁体   中英

Python: Write dictionary to text file with ordered columns

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!

Now, an alternate, easier and more efficient way of doing this, by using the wonderful Pandas library

import pandas as pd

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

df = pd.DataFrame(D).T.reindex(columns=order)

df.to_csv('./foo.txt', sep='\t', na_rep="none")

Result:

$ python test1.py
$ cat foo.txt
        mix     meow
bar     none    4.56
baz     none    none
foo     2.34    1.23

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()):"

This is what I came up with from your code:

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 order:    
        head.append('{!s:{}}'.format(i,width))

    writer.writerow(head)

    for i in sorted(D.keys()):
      row = ['{!s:{}}'.format(i,width)]
      for k in order: row.append('{!s:{}}'.format(D[i][k],width))
      writer.writerow(row)

File content:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM