473,802 Members | 2,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using difflib to compare text ignoring whitespace differences

Hi

I'm trying to compare some text to find differences other than whitespace.
I seem to be misunderstandin g something, since I can't even get a basic
example to work:

In [104]: d = difflib.Differ( charjunk=diffli b.IS_CHARACTER_ JUNK)

In [105]: list(d.compare([' a'], ['a']))
Out[105]: ['- a', '+ a']

Surely if whitespace characters are being ignored those two strings should
be marked as identical? What am I doing wrong?

Thanks
Neilen

--
you know its kind of tragic
we live in the new world
but we've lost the magic
-- Battery 9 (www.battery9.co.za)

Dec 19 '06 #1
1 9493
On 19 dic, 11:53, Neilen Marais <nmar...@sun.ac .zawrote:
Hi

I'm trying to compare some text to find differences other than whitespace.
I seem to be misunderstandin g something, since I can't even get a basic
example to work:

In [104]: d =difflib.Differ (charjunk=diffl ib.IS_CHARACTER _JUNK)

In [105]: list(d.compare([' a'], ['a']))
Out[105]: ['- a', '+ a']

Surely if whitespace characters are being ignored those two strings should
be marked as identical? What am I doing wrong?
The docs for Differ are a bit terse and misleading.
compare() does a two-level matching: first, on a *line* level,
considering only the linejunk parameter. And then, for each pair of
similar lines found on the first stage, it does a intraline match
considering only the charjunk parameter.
Also note that junk!=ignored, the algorithm tries to "find the longest
contiguous matching subsequence that contains no ``junk'' elements"

Using a slightly longer text gets closer to what you want, I think:

d=difflib.Diffe r(charjunk=diff lib.IS_CHARACTE R_JUNK)
for delta in d.compare([' a larger line'],['a longer line']): print
delta

- a larger line
? --- ^^

+ a longer line
? ^^

--
Gabriel Genellina

Dec 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3216
by: Russell | last post by:
I'm trying to automate the comparison of some source code but I'm having trouble using the differ object. I'm new to Python so I'm probably doing something wrong. I've created a small program (included below) that demonstrates my problem. For some reason, Python doesn't recognize differ. I keep getting this error: NameError: name 'differ' is not defined. What am I doing wrong? Test Code follows: import difflib
3
1724
by: Humpdydum | last post by:
Can anyone try the following in their python interpreter? These give correct output: >>> print list(ndiff(,)) >>> print list(ndiff(,)) >>> print list(ndiff(,))
2
3223
by: RobG | last post by:
Why does Firefox insert #text nodes as children of TR elements? As a work-around for older Safari versions not properly supporting a table row's cells collection, I used the row's childNodes collection as it was pretty much exactly the same thing. However, in Firefox 1.0.7 text nodes are inserted between the TDs. I'm certain that this didn't use to happen with older versions. The HTML specification states that the only element that...
11
8731
by: John Henry | last post by:
I am just wondering what's with get_close_matches() in difflib. What's the magic? How fuzzy do I need to get in order to get a match?
2
1927
by: veinnz | last post by:
hi all... i'm kinda newbie in php...so i would like to ask ur guidance on how to compare the distribution of whitespace between two text area? so let's say text area 1 i type : hi how are you text area 2 i type : you've got it right both has the same distribution of white space but since php ignore the whitespace.. i've this idea is by replacing all the whitespace into something else 1st...let's say to "@".. bt then i've problem on how...
7
3431
by: whitewave | last post by:
Hi Guys, I'm a bit confused in difflib. In most cases, the differences found using difflib works well but when I have come across the following set of text: .... problem even for the simple forms of the differential equation and the nonlocal conditions. Due to these facts, some serious difficulties arise in the application of the classical methods to such a problem.''' .... adjoint problem even for the simple forms of the differential
2
1346
by: krishnakant Mane | last post by:
hello all, I have a bit of a confusing question. firstly I wanted a library which can do an svn like diff with two files. let's say I have file1 and file2 where file2 contains some thing which file1 does not have. now if I do readlines() on both the files, I have a list of all the lines. I now want to do a diff and find out which word is added or deleted or changed. and that too on which character, if not at least want to know the word...
3
1841
by: n00m | last post by:
from random import randint s1 = '' s2 = '' for i in xrange(1000): s1 += chr(randint(97,122)) s2 += chr(randint(97,122)) print s1
0
888
by: Gabriel Genellina | last post by:
En Thu, 25 Sep 2008 05:30:41 -0300, <dudeja.rajat@gmail.comescribió: Instead of parsing that generated file (intended for human usage), use difflib.Differ or difflib.SequenceMatcher - you get a programming interface to the same info, easier to use in a script. -- Gabriel Genellina
0
10532
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10281
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10058
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6835
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3789
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.