Note: Forwarded message attached

-- Original Message --

From: "mannu jha"mannu_0...@rediffmail.com
To: tuomas.vesteri...@iki.fi
Subject: Re: Re: joining files
--- Begin Message ---
> import os
> def merge_sources(sources):
>   # sources is a list of tuples (source_name, source_data)
>   data = []
>   keysets = []
>   for nme, sce in sources:
>    lines = {}
>    for line in sce.split(os.linesep):
>     lst = line.split()
>     lines[lst[0]] = (nme, lst)
>    keysets.append(set(lines.keys()))
>    data.append(lines)
>   common_keys = keysets[0]
>   for keys in keysets[1:]:
>    common_keys = common_keys.intersection(keys)
>   result = {}
>   for key in common_keys:
>    result[key] = dict(d[key] for d in data if key in d)
>   return result
> if __name__ == "__main__":
>   # Your test files here are replaced by local strings
>   print merge_sources([("file1", file1), ("file2", file2), ("file3",

> file3)])
>   print merge_sources([("input1", input1), ("input2", input2)])
> Test_results = '''
> {'22': {'file3': ['22', 'C'],
>    'file2': ['22', '0'],
>    'file1': ['22', '110.1', '33', '331.5', '22.7', '5', '271.9'
>      '17.2', '33.4']}}
> {'194': {'input2': ['194', 'C'],
>    'input1': ['194', '8.00', '121.23', '54.79', '4.12',
>       '180.06']},
>  '175': {'input2': ['175', 'H', '176', 'H', '180', 'H'],
>    'input1': ['175', '8.42', '120.50', '55.31', '4.04',
>       '180.33']},
>  '15': {'input2': ['15', 'H', '37', 'H', '95', 'T'],
>    'input1': ['15', '8.45', '119.04', '55.02', '4.08',
>       '178.89']},
>  '187': {'input2': ['187', 'H', '190', 'T'],
>    'input1': ['187', '7.79', '122.27', '54.37', '4.26',
>       '179.75']}}
> Dear Sir,

> I tried above program but with that it is showing error:
> nmru...@caf:~> python join1.py
> Traceback (most recent call last):
>  File "join1.py", line 24, in
>   print merge_sources([("file1", file1), ("file2", file2), ("file3",file3)])
> NameError: name 'file1' is not defined
> nmru...@caf:~
Add test data to the code as:
file1 = '''22 110.1 33 331.5 22.7 5 271.9 17.2 33.4
4 55.1'''
Thankyou very much sir it is working......Thankyou once again for your kind 
help. 

only one problem I am now facing i.e. when I tried to replace test data with 
filename 1.e.
    file1 = open("input11.txt")
    file2 = open("output22.txt")
    print merge_sources([("file1", file1), ("file2", file2)])
then it is showing error 

ph08...@linux-af0n:~> python new.py
Traceback (most recent call last):
  File "new.py", line 25, in 
    print merge_sources([("file1", file1), ("file2", file2)])
  File "new.py", line 9, in merge_sources
    for line in sce.split(os.linesep):
AttributeError: 'file' object has no attribute 'split'
ph08...@linux-af0n:~> 

where my input11.txt is:
'''187 7.79 122.27 54.37 4.26 179.75
194 8.00 121.23 54.79 4.12 180.06
15 8.45 119.04 55.02 4.08 178.89
176 7.78 118.68 54.57 4.20 181.06
180 7.50 119.21 53.93 179.80
190 7.58 120.44 54.62 4.25 180.02
152 8.39 120.63 55.10 4.15 179.10
154 7.79 119.62 54.47 4.22 180.46
175 8.42 120.50 55.31 4.04 180.33'''

and output22.txt is:
'''15 H
37 H
95 T
124 H
130 H
152 H
154 H
158 H
164 H
175 H
176 H
180 H
187 H
190 T
194 C'''

since my files are very big hence i want to give filename as input.






--- End Message ---
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to