I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.
Rather than re-invent the wheel I am wondering if anyone has written
anything already? 12 5841
On May 29, 6:36 pm, loial <jldunn2...@googlemail.comwrote:
I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.
Rather than re-invent the wheel I am wondering if anyone has written
anything already?
You can use the cmp(x, y) function to tell if a string is similar to
another.
going cmp('spam', 'eggs') will return 1 (spam is greater than eggs)
(have no idea why)
swapping the two give -1
and having 'eggs' and 'eggs' gives 0.
is that what you were looking for?
Kalibr wrote:
On May 29, 6:36 pm, loial <jldunn2...@googlemail.comwrote:
>I have a requirement to compare 2 text files and write to a 3rd file only those lines that appear in the 2nd file but not in the 1st file.
Rather than re-invent the wheel I am wondering if anyone has written anything already?
You can use the cmp(x, y) function to tell if a string is similar to
another.
Or "==".
Stefan
loial wrote:
I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.
lines_in_file2 = set(open("file2").readlines())
for line in open("file1"):
if line not in lines_in_file2:
print line
Stefan
On May 29, 10:36*am, loial <jldunn2...@googlemail.comwrote:
I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.
Rather than re-invent the wheel I am wondering if anyone has written
anything already?
How large are the files ? You could load up the smallest file into
memory then while iterating over the other one just do 'if line in
other_files_lines:' and do your processing from there. By your
description it doesn't sound like you want to iterate over both files
simultaneously and do a line for line comparison because that would
mean if someone plonks an extra newline somewhere it wouldn't gel.
Another way of doing this might be to use the module difflib to
calculate the differences. It has a sequence matcher under it which
has the function get_matching_blocks
difflib is included with python.
On May 29, 2:02*pm, Chris <cwi...@gmail.comwrote:
On May 29, 10:36*am, loial <jldunn2...@googlemail.comwrote:
I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.
Rather than re-invent the wheel I am wondering if anyone has written
anything already?
How large are the files ? You could load up the smallest file into
memory then while iterating over the other one just do 'if line in
other_files_lines:' and do your processing from there. *By your
description it doesn't sound like you want to iterate over both files
simultaneously and do a line for line comparison because that would
mean if someone plonks an extra newline somewhere it wouldn't gel.
On May 29, 6:36 pm, loial <jldunn2...@googlemail.comwrote:
I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.
Rather than re-invent the wheel I am wondering if anyone has written
anything already?
>>file1 = set((x for x in open('file1'))) file2 = set((x for x in open('file2'))) file3 = file2.difference(file1) open('file3','w').writelines(file3)
On May 29, 1:36*am, loial <jldunn2...@googlemail.comwrote:
only those lines that appear in the 2nd file but not in the 1st file.
set(file_2_recs).difference(set(file_1_recs)) will give the recs in
file_2 that are not in file_1 if you can store both files in memory.
Sets are indexed and so are faster than lists.
Open('3rd', 'w').writelines(set(open('2nd').readlines())-set(open('1st')))
2008/5/29, loial <jl********@googlemail.com>:
I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.
Rather than re-invent the wheel I am wondering if anyone has written
anything already?
-- http://mail.python.org/mailman/listinfo/python-list
--
mvh Björn
2008/5/29, loial <jl********@googlemail.com>:
>I have a requirement to compare 2 text files and write to a 3rd file only those lines that appear in the 2nd file but not in the 1st file.
En Thu, 29 May 2008 18:08:28 -0300, BJörn Lindqvist <bj*****@gmail.com>
escribió:
Open('3rd','w').writelines(set(open('2nd').readlin es())-set(open('1st')))
Is the asymmetry 1st/2nd intentional? I think one could omit .readlines()
in 2nd file too.
--
Gabriel Genellina
On May 29, 3:36*am, loial <jldunn2...@googlemail.comwrote:
I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.
Rather than re-invent the wheel I am wondering if anyone has written
anything already?
Take the time to learn difflib - it is a standard module, and good for
general comparison of files, sequences, etc.
-- Paul
On Thu, 29 May 2008 01:36:44 -0700, loial wrote:
I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.
Rather than re-invent the wheel I am wondering if anyone has written
anything already?
Of course you can do this at any linux or unix command line simply by:
comm -13 file1 file2 >file3
On May 29, 3:36*pm, loial <jldunn2...@googlemail.comwrote:
I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.
Rather than re-invent the wheel I am wondering if anyone has written
anything already?
It's so easy to do that it won't count as reinventing the wheel:
a = open('a.txt', 'r').read().split('\n')
b = open('b.txt', 'r').read().split('\n')
c = open('c.txt', 'w')
c.write('\n'.join([comm for comm in b if not (comm in a)]))
c.close()
it's not the fastest common searcher but it works. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: g |
last post by:
I'd like to have a CSS file for everything that's common to my site,
and other CSS files that are specific to certain pages or groups of
pages. When a page loads, it would load in BOTH the General...
|
by: PyPK |
last post by:
I have two files
file1 in format
<id> <val1> <test1> <test2>
'AA' 1 T T
'AB' 1 T F
file2 same as file1
<id> <val1> <test1> <test2>
'AA' 1 T T
'AB' 1 T T
|
by: SP |
last post by:
Hi All,
I need to compare two files line by line and copy the differing lines to a
new files. I.e. Compare file1 and file2 line by line. Copy only differing
lines to file3.
I tried a couple...
|
by: Morten Snedker |
last post by:
Hi folks,
Every 14 days a production file is read into a SQL-server. Each line
must be handled individually and managed by code.
The last file contains approx. 610,000 lines and gains +40,000...
|
by: Fazana |
last post by:
I need help on how to read files into a program. For example, there are three lines of code into a text file. I have read all the lines but have to discard the first two lines.
Can anyone tell me...
|
by: anithas |
last post by:
I need to write a C program to compare two files not in the same sequence.
ie. Take one line from first file....compare with all lines in second file....If there is no match....put it in a third...
|
by: KevinADC |
last post by:
Note: You may skip to the end of the article if all you want is the perl code.
Introduction
Uploading files from a local computer to a remote web server has many useful purposes, the most...
|
by: aRTx |
last post by:
I have try a couple of time but does not work for me
My files everytime are sortet by NAME.
I want to Sort my files by Date-desc.
Can anyone help me to do it?
The Script
<?
/*
ORIGJINALI
|
by: zw |
last post by:
Hi
I have 2 log files, each with a timestamp on the first 2 fields.
However, when I do a
awk '/ / {print $1,$2}' logs/x.log
on a log file,
it is complicated by the fact that I also get other...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |