jarod...@libero.it wrote: > Hi I want to merge many files like this: > #file1 > A 10 > B 20 > C 30 > #file2 > B 45 > Z 10 > #file1 > A 60 > B 70 > C 10 > > I want to obtain > > A 10 0 60 > B 20 45 70 > C 30 0 10 > Z 0 10 0 > > I try to do like this: > f = os.listdir(".") > for i in f: > T =open(i,"r") > for r in t.readlines(): > print r > > Could you please help me to understand how to use the right procedure in > python?Thanks a lot!
If I were into spoon-feeding I would post import os folder = "." destfilename = "../pivot" filenames = sorted(os.listdir(folder)) filenames = [os.path.join(folder, name) for name in filenames] N = len(filenames) pivot = {} for index, filename in enumerate(filenames): with open(filename) as file: for line in file: key, value = line.split() pivot.setdefault(key, ["0"] * N)[index] = value with open(destfilename, "w") as dest: for key, value in sorted(pivot.items()): dest.write("\t".join([key] + value)) dest.write("\n") _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor