简体   繁体   English

将字典词典转换为选项卡文件的最佳方法

[英]Best Way to Convert a Dictionary of Dictionaries into a tab file

What is the most pythonic way to convert a dictionary of dictionaries into a tab delimited file? 将字典词典转换为制表符分隔文件的最有效方法是什么? I have the script below that produces a dictionary of dictionaries in the form of 我下面的脚本生成以下形式的词典字典:

{'name1:[{'AA':2,'GG':3,'CF':10}], name2:[{'AA':4, 'GG':9,'CF':5,'GT':1}]}

I am looking for a way to get an output from there that looks like the following(tab delimated): 我正在寻找一种从那里获取输出的方法,该输出如下所示:

titles   AA  GG  CF  GT
name1    2   3   10  0
name2    4   9   5   1

I haven't been able to figure out a convenient way to do it. 我还没有办法找到一种方便的方法。 This is the script I used to pull frequencies counts from a file. 这是我用来从文件中提取频率计数的脚本。

def kmer_counts(b,link):
kmer_dict = {}
values = []
for filename in os.listdir(b):
    if str(filename).endswith(link):
    for record in SeqIO.parse(os.path.join(b, filename), "fasta"):
        id_=record.id
        seq = record.seq
        mylist = kmer_list(seq,4)
        c = collections.Counter(mylist)
        c = dict(c)
        val = list(c.values())
        keys = []
        for key in c:
            keys.append(str(key))
        c_edit = dict(zip(keys,val))
        kmer_dict.setdefault(str(id_),[])
        kmer_dict[str(id_)].append(c_edit)
print kmer_dict

*Note I amended the script because someone pointed out it wasn't outputting the right thing. *注意,我修改了脚本,因为有人指出它没有输出正确的内容。

There's not a super-ultra-elegant approach, given that your data is not shaped like a csv, but this isn't going to require all that much code. 鉴于您的数据的形状不像csv,因此没有超级优雅的方法,但这并不需要那么多的代码。

I note that this would be very slightly nicer if your data looked like this: 我注意到,如果您的数据看起来像这样,那就更好了:

{'name1':{'AA':2,'GG':3,'CF':10}, 'name2':{'AA':4, 'GG':9,'CF':5,'GT':1}}

instead of this: 代替这个:

{'name1':[{'AA':2,'GG':3,'CF':10}], 'name2':[{'AA':4, 'GG':9,'CF':5,'GT':1}]}
         ^                       ^          ^                              ^

Anyway: 无论如何:

import csv
import sys

# your data
data = {'name1':[{'AA':2,'GG':3,'CF':10}], 'name2':[{'AA':4, 'GG':9,'CF':5,'GT':1}]}
topleft = 'titles' # whatever you want the leftmost column header to be

# identify the column headers, and put them in alphabetical order (or some other order, if you'd prefer that)
headers = sorted(set(key
                     for row in data.values()
                     for key in row[0]))

# open a csv writer - this one writes to sys.stdout for interactive convenience
writer = csv.writer(sys.stdout, delimiter='\t')

# write the headers
writer.writerow([topleft] + headers)

for key in data:
    # construct each row
    row = [key] # row header
    for header in headers:
        row.append(data[key][0].get(header, 0)) # use 0 if a given header is absent in a given row
    writer.writerow(row) # write the row

Output: 输出:

titles  AA  CF  GG  GT
name1   2   10  3   0
name2   4   5   9   1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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