![](/img/trans.png)
[英]Writing list of dictionaries to CSV with each dictionary's values in a new column
[英]python; merge dictionaries with each dictionary in a new column of the output csv file
使用以下腳本,我將3個文件解析為python中的一個字典。 詞典沒有所有相似的鍵,我想在輸出的csv文件的新列中添加每個詞典的值。 因此,鍵必須全部放在一列中,然后是每列都包含不同字典值的列。 我的腳本的問題是僅在存在值的情況下附加值,結果是不同字典的值位於輸出csv文件的錯誤列中。 我的腳本如下:
def get_file_values(find_files, output_name):
for root, dirs, files in os.walk(os.getcwd()):
if all(x in files for x in find_files):
outputs = []
for f in find_files:
d = {}
with open(os.path.join(root, f), 'r') as f1:
for line in f1:
ta = line.split()
d[ta[1]] = int(ta[0])
outputs.append(d)
d3 = defaultdict(list)
for k, v in chain(*(d.items() for d in outputs)):
d3[k].append(v)
with open(os.path.join(root, output_name), 'w+', newline='') as fnew:
writer = csv.writer(fnew)
writer.writerow(["genome", "contig", "genes", "SCM", "plasmidgenes"])
for k, v in d3.items():
fnew.write(os.path.basename(root) + ',')
writer.writerow([k] + v)
print(d3)
get_file_values(['genes.faa.genespercontig.csv', 'hmmer.analyze.txt.results.txt', 'genes.fna.blast_dbplasmid.out'], 'output_contigs_SCMgenes.csv')
我現在的輸出是:
genome contig genes SCM plasmidgenes
Linda 9 359 295 42
Linda 42 1 2
Linda 73 29 5
Linda 43 17 6
Linda 74 4
Linda 48 11
Linda 66 27
我想擁有它;
genome contig genes SCM plasmidgenes
Linda 9 359 295 42
Linda 42 1 2 0
Linda 73 0 29 5
Linda 43 17 0 6
Linda 74 0 0 4
Linda 48 0 11 0
Linda 66 27 0 0
最簡單的解決方法:檢查該值是否存在,是否將其追加,否則將0追加到數據數組。
可能是更復雜的修復:使用其他數據結構,例如Pandas或類似於數據的二維數組。
二維數組的示例:
您將首先遍歷文件,並用d3[lineNumber][key]
填充d3數組。 例如d3[0]['genome']
將是您的第一行第一列。
然后,您應該可以使用以下代碼塊輸出文件:
with open(os.path.join(root, output_name), 'w+', newline='') as fnew:
writer = csv.writer(fnew)
# write header row
header = ""
for k, v in d3[0].items():
header += k + ','
writer.writerow(header)
# write data rows
for key, row in d3.items():
line = ""
line += os.path.basename(root)
for k, v in row.items():
line += ',' + v
writer.writerow(line)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.