0

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?

4
  • It is almost impossible to figure out the content of the text files from the above description. Try formatting it as code (four spaces in front of each line). Commented Mar 26, 2010 at 12:58
  • Please define the way the files are "compared". It looks more like a merging process, with some odd rules about what lines are kept are what lines are excluded. For example, what happened to the "login;user" lines ? If a merge (or a merge with some exclusion rules) is effectively required, can we assume the lines in each file are sorted ? Commented Mar 26, 2010 at 13:06
  • Looks like a line by line "word-wise" OR. Commented Mar 26, 2010 at 13:06
  • 1
    @waffleman: quite possibly! Let's see if noname cares to specify. No point in guessing with such a small sample; could be just about any rule (for ex. SilentGhost's awswer would also fit the sample, if only making several assumption such as "priority" for a, 3 fields expected etc.) Commented Mar 26, 2010 at 13:25

3 Answers 3

4
for a, b in zip(open('a'), open('b')):
    print(a if len(a.split(';')) == 3 else b)
Sign up to request clarification or add additional context in comments.

4 Comments

+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.
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: it is iterator, in py3k
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
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.