[Tutor] Comparing two different text file
Steven D'Aprano
steve at pearwood.info
Wed Jul 30 02:34:03 CEST 2014
On Wed, Jul 30, 2014 at 08:13:37AM +0900, 이명교 wrote:
> #!/usr/bin/env python
> list1 = []
> list2 = []
> list3 = []
> list4 = []
> #imports list from first text file
> inf1 = open('first.txt')
> for line in inf1.readlines():
> list1.append(line)
> list1 = line[:-1].split('\n')
Try this:
infile = open('first.txt')
list1 = infile.readlines()
infile.close()
The readlines() method already returns a list of all the lines in the
file, so there's no need to iterate over the lines a second time just to
put them into a list.
Now do the same for 'second.txt' to get the lines from it. No need to
have separate "infile1" and "infile2" variables, you can re-use the
variable. (But don't forget to close the files after you've read them.)
So now you have two lists, list1 and list2, containing the lines from
the two files.
> #append items of list1
> for a in list1:
> if a not in list1:
> list3.append(a)
That cannot possibly do anything. You are testing each line of list1, to
see if it *isn't* from list1. Of course it will be from list1.
I *think* that what you want is to make a copy of list1 and put it in
list3:
list3 = []
for line in list1:
list3.append(line)
But wait! There's an easier way: list slicing.
https://docs.python.org/2/tutorial/introduction.html#lists
So you ought to be able to make a copy of list1 in a single line.
Then you can walk over each line of list2, check whether or not that
line is in list1 (NOT list2!!!), and append to list3:
for line in list2:
if line not in list1:
list3.append(line)
P.S. no need to show us dozens and dozens of lines of data. Just show us
a few examples, say three or five, not fifty or a hundred.
--
Steven
More information about the Tutor
mailing list