dictionary to csv

#!/usr/bin/env python
import os,re,sys,string,commands,getopt,subprocess,glob,csv
import prettytable as pt
from os import path
#SL335752_kneaddata_paired_1_kneaddata_paired_1.fastq.gz_rep.mpa.txt_bracken.txt
#SL311013_1_kneaddata_paired_1_kneaddata_paired_1.fastq.gz_rep_mpa.txt_bracken.txt
def main():
dic = {}
unique_speciesname = []
speciesname = []
samplenames = []
for d in os.listdir(‘.’):
print(d)
a = []
a = re.split(“_kneaddata_paired_1_”,d)
if len(a) >=2:
samplename = a[0]
dic[a[0]] = {}
#os.rename(d, newname)
fh = open(d, ‘r’)
fhlines = fh.readlines()
fh.close()

for line in fhlines:
line = line.strip()
if re.search(“name”, line):
continue
else:
b = []
b = re.split(“\t”,line)
speciesname.append(b[0])
length = len(b)
dic[a[0]][b[0]] = b[length-1]
unique = [unique_speciesname.append(x) for x in speciesname if x not in unique_speciesname]
for sample_name in dic.keys():
samplenames.append(sample_name)
for name in unique_speciesname:
#print name
if dic[sample_name].has_key(name):
print_line = name + “\t” + dic[sample_name][name]
else:
dic[sample_name][name] = “0”
print_line = name + “\t” + dic[sample_name][name]

#print dic
csv_columns = [‘Species’]
for ele in samplenames:
csv_columns.append(ele)
print csv_columns
#csv_columns = [‘No’,’Name’,’Country’]
csv_file = “sample_species.csv”
#{‘SL311014′: {‘Acinetobacter sp. WCHA55′: ‘0.00029’, ‘Streptococcus sp. oral taxon 431′: ‘0.00007’, ‘Bacillus velezensis': ‘0.00001’, ‘Ahniella affigens': ‘0.00003’, ‘Arsenicicoccus sp. oral taxon 190′: ‘0.00077’, ‘Aureimonas sp. AU20′: ‘0’, ‘Mycobacterium sp. MS1601′: ‘0’, ‘Acinetobacter sp. WCHAc010052′: ‘0.00003’, ‘Candidatus Micrarchaeota archaeon Mia14′: ‘0.00000’, ‘Pseudomonas sp. MT-1′: ‘0.00005’, ‘Halothece sp. PCC 7418′: ‘0.00001’}, ‘SL311013′: {‘Acinetobacter sp. WCHA55′: ‘0’, ‘Streptococcus sp. oral taxon 431′: ‘0.00009’, ‘Bacillus velezensis': ‘0.00004’, ‘Candidatus Micrarchaeota archaeon Mia14′: ‘0’, ‘Ahniella affigens': ‘0.00002’, ‘Arsenicicoccus sp. oral taxon 190′: ‘0.00059’, ‘Mycobacterium sp. MS1601′: ‘0.00029’, ‘Acinetobacter sp. WCHAc010052′: ‘0.00001’, ‘Aureimonas sp. AU20′: ‘0.00026’, ‘Pseudomonas sp. MT-1′: ‘0.00003’, ‘Halothece sp. PCC 7418′: ‘0.00001’}, ‘SL311012′: {‘Acinetobacter sp. WCHA55′: ‘0’, ‘Streptococcus sp. oral taxon 431′: ‘0.00008’, ‘Bacillus velezensis': ‘0.00001’, ‘Candidatus Micrarchaeota archaeon Mia14′: ‘0’, ‘Ahniella affigens': ‘0.00001’, ‘Arsenicicoccus sp. oral taxon 190′: ‘0.00054’, ‘Mycobacterium sp. MS1601′: ‘0.00026’, ‘Acinetobacter sp. WCHAc010052′: ‘0.00002’, ‘Aureimonas sp. AU20′: ‘0.00024’, ‘Pseudomonas sp. MT-1′: ‘0.00003’, ‘Halothece sp. PCC 7418′: ‘0.00000’}}
dict_data_mine = []
#dic_ele = {}
for name in unique_speciesname:
dic_ele = {}
dic_ele[“Species”]=name
for key in dic.keys():
#dic_ele[“speciesname”]=>name
dic_ele[key]=dic[key][name]
dict_data_mine.append(dic_ele)
dict_data = [
{‘No': 1, ‘Name': ‘Alex’, ‘Country': ‘India’},
{‘No': 2, ‘Name': ‘Ben’, ‘Country': ‘USA’},
{‘No': 3, ‘Name': ‘Shri Ram’, ‘Country': ‘India’},
{‘No': 4, ‘Name': ‘Smith’, ‘Country': ‘USA’},
{‘No': 5, ‘Name': ‘Yuva Raj’, ‘Country': ‘India’},
]

#print dict_data_mine
try:
with open(csv_file, ‘w’) as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in dict_data_mine:
writer.writerow(data)
except IOError:
print(“I/O error”)
if __name__== ‘__main__':
main()

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Protected by WP Anti Spam