473,804 Members | 2,119 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 9496
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
3225
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
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
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,...
1
7642
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6867
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
5535
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...
1
4313
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
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
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.