On Thu, Nov 14, 2013 at 8:26 AM, jarod...@libero.it <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
>From your example, it looks like you can have any number of unique letters and the final file should have each of those listed. Against each, you should have the numbers that appear against them in the files (0 if not). So here is what I think should be an approach you could follow: 1. For each file, record each letter and the number against it (A dictionary comes to mind) 2. Once you have the dictionary for each file, create a set (as in a Python data structure) of the keys. This set will give you all the unique letters. 3. Now for each member of this set, look if it appears it each of the above dictionaries. If it occurs, note it's value, else put it down as 0. Create a new dictionary (which will be your final dictionary). The key should be each letter and the value should be a list with one or more numbers. 4. Once you are done iterating over each element of the set, your dictionary in step 3 will have the unique letters against the numbers that occur against them in the files. Now, you can write this dictionary into a file. Since you want a plain text file you will have to iterate over the keys and values. So let's consider your example: > #file1 > A 10 > B 20 > C 30 > #file2 > B 45 > Z 10 > #file3 > A 60 > B 70 > C 10 After step 1 above, you have three dictionaries, say d1, d2 and d3: d1 = {'A':10, 'B': 20, 'C':30} d2 = {'B':45, 'Z':10} d3 = {'A':60, 'B':70, 'C':10} Now, step 2, will give you a set of the keys above: unique_letters = {'A', 'B', 'C', 'Z'} After step 3, you will have this global dictionary: d = {'A':[10, 0, 60], 'B':[20, 45, 70], 'C':[30, 0, 10], 'Z':[0, 10, 0]} Now you can write this dictionary to a file. Note that step 2 will require you to combine the three individual dictionaries, so you may have to learn how to do that first. > > 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! Does the above steps help? Best, Amit. -- http://echorand.me _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor