多个相同行列文件的合并

#! /usr/bin/python
 
import glob

out_file1 = open(r"E:\\Git-2.11.1-64-bit\\Git\\localhost\\lianshou\\4WGCNA\\merge.txt", "w")
 
MyDiction = {}
 
def Readfile(File):
    file_obj = open(File)
    try:
        while True:
            line = file_obj.readline().strip("\n")
            if not line:
                break
            array = line.split("\t")
            if array[0] in MyDiction:
                MyDiction[array[0]].append(array[1])
            else:
                MyDiction[array[0]] = [array[1]]
    finally:
        file_obj.close()
    return MyDiction
 
def main():
    list_dirs = glob.glob("./*.txt")
    for i in list_dirs:
        Readfile(i)
    for gene_name in MyDiction:
        #print ("%s\t%s" %(gene_name, "\t".join(MyDiction[gene_name]))) 
        out_file1.write("%s\t%s%s" %(gene_name, "\t".join(MyDiction[gene_name]),"\n"))
 
if __name__ == '__main__':
    main()

脚本来自于生信技能树很实用,学习到了glob包的妙用。


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *