I have 29 text files as follows

File 1

12313 : 23546

        12313

        23214

        32465

File 2

13132 : 23546

        12323

        32125

        32125

        32121

        .

        .

        .

I would like to have python script which computes intersection between these files

ideal result as follow

12313 : 23546

13132 : 23546

Recommended Answers

All 4 Replies

You can use set and & for intersection:

d1 = """
12313 : 23546

        12313

        23214

        32465
"""

d2 = """
13132 : 23546

        12323

        32125

        32125

        32121
"""


ds1 = set(line[8:].strip() for line in d1.splitlines() if line)
ds2 = set(line[8:].strip() for line in d2.splitlines() if line)

print ds1 & ds2

""" Output:
set(['23546'])
"""

Hi pyTony,

Thank you very much for reply but i have 29 files i have like that how should i store files and compute intersection between all files

And finally want to print first column ID and common id between files as follows

12313 : 23546

13132 : 23546

Can you please suggest how to do it

Thanks in advance

Ni

Sorry , we help people to learn but do not do their home works for them.

Check the difflib and filecmp modules in the Python manual.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.