Reputation: 1205
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!
Upvotes: 0
Views: 3527
Reputation: 8447
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
Upvotes: 1
Reputation: 8447
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
Upvotes: 0
Reputation: 1910
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()):"
Upvotes: 0