views:

171

answers:

3

in a.txt i have the text(line one after the other)

login;user;name
login;user;name1
login;user

in b.txt i have the text

login;user
login;user
login;user;name2

after comparing it should display in a text file as

login;user;name
login;user;name1
login;user;name2.... 

How can it be done using python?

+1  A: 

Perhaps the standard-lib difflib module can be of help - check out its documentation. Your question is not clear enough for a more complete answer.

Eli Bendersky
+3  A: 
for a, b in zip(open('a'), open('b')):
    print(a if len(a.split(';')) == 3 else b)
SilentGhost
+1. Note that this solution assumes the login;user portion of the lines are in the same order. If they are not, you will need to reorder them (e.g. by sorting by line) or create a dictionary. The latter technique is more complex and only needed if there are additional constraints not mentioned by the OP.
Brian
With what appears to be log files (hence "big" files), it might be advisable to drive the loop with iterator, using itertools.izip(), rather than building a possibly huge sequence first.
mjv
@mjv: it is iterator, in py3k
SilentGhost
@SilentGhost, quite right, _in py3k_; I should have qualified "If you're in Py 2.x..." Indeed this and other py3k improvements, along with the porting of critical libraries, will eventually (I think soon) "tip the balance", but until then, the bulk of Python coding seems to take place in 2.x
mjv
A: 

Based on the vague information given, I would try something like the following:

import itertools

def merger(fni1, fni2):
    "merge two files ignoring 'login;user\n' lines"
    fp1= open(fni1, "r")
    fp2= open(fni2, "r")
    try:
        for line in itertools.chain(fp1, fp2):
            if line != "login;user\n":
                yield line
    finally:
        fp1.close()
        fp2.close()

def merge_to_file(fni1, fni2, fno):
    with open(fno, "w") as fp:
        fp.writelines(merger(fni1, fni2))

The merge_to_file is the function you should use.

ΤΖΩΤΖΙΟΥ