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 5837
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: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |