473,467 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Printing duplicates from a text file

6 New Member
If i have a text file 'test.txt' containing:

1
1
1
2
2
3

I want it to print all the lines that are duplicates/matches.

So I would expect an output of

1
1
1
2
2

I was thinking something along the lines of:
Expand|Select|Wrap|Line Numbers
  1. test = open("test.txt", 'r')
  2.  
  3. for x in test:
  4.      for y in test:
  5.          if x==y:
  6.             print x
  7.             print y
But it doesn't work.

Please can someone advise?
May 22 '12 #1

✓ answered by bvdet

Read the file and compile a list of the items in the file.
Iterate on the list.
Use list method count to determine if there are duplicates.

Example:
Expand|Select|Wrap|Line Numbers
  1. >>> seq = ['1', '1', '2', '2', '2', '3']
  2. >>> for item in seq:
  3. ...     if seq.count(item) > 1:
  4. ...         print item
  5. ...         
  6. 1
  7. 1
  8. 2
  9. 2
  10. 2
  11. >>> 

3 9908
bvdet
2,851 Recognized Expert Moderator Specialist
Read the file and compile a list of the items in the file.
Iterate on the list.
Use list method count to determine if there are duplicates.

Example:
Expand|Select|Wrap|Line Numbers
  1. >>> seq = ['1', '1', '2', '2', '2', '3']
  2. >>> for item in seq:
  3. ...     if seq.count(item) > 1:
  4. ...         print item
  5. ...         
  6. 1
  7. 1
  8. 2
  9. 2
  10. 2
  11. >>> 
May 22 '12 #2
dwblas
626 Recognized Expert Contributor
For larger files, store the records found in a set so you only iterate once.
Expand|Select|Wrap|Line Numbers
  1. x_set=set()
  2. for x in test:
  3.     x=x.strip()
  4.     if x in x_set:
  5.         print x
  6.     else:
  7.         x_set.add(x) 
The code you posted would iterate over the list once for each successive record and would be written:
Expand|Select|Wrap|Line Numbers
  1. test = open("test.txt", 'r')
  2.  
  3. all_recs=test.readlines()
  4. test.close()
  5.  
  6. for ctr in range(len(all_recs)):
  7.     x = all_recs[ctr].strip()
  8.     for y in range(ctr+1, len(all_recs):  ## start with the next record
  9.         if x==all_recs[y].strip():
  10.             print y, all_recs[y] 
May 22 '12 #3
pystarter
6 New Member
Thanks guys - great help!
May 23 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Serge Guay | last post by:
I have been trying to print a text file to my printer but the most I have been able to do is print one line. I have been using the following commands. dc = win32ui.CreateDC()...
2
by: dw | last post by:
Hello all. We have an interesting situation: A developer wants to allow users to print a text-only version of an ASP page -- but do so without showing the page. In other words, the users are...
2
by: Ron | last post by:
Hi All Okay, I've got a report that runs perfectly. Designed originally with the report wizard and enhanced with about 8 subreports and other stuff. Works great. However, I also now need to...
19
by: Materialised | last post by:
Hi everyone, What I am wanting to do, is to copy, a simple plain text file, to another file, but omitting duplicate items. The way I thought of doing this, involved copying all the items into...
2
by: Benny | last post by:
Hello Experts, Currently I working on a web application using vs.net with C#. I require to create an invoice to a text file and print the file. I have no problem with writing to the text, but...
2
by: David Cuffee | last post by:
I am not having any luck printing a simple text file with PCL codes to my windows printer. If I have a text file with PCL codes and I want to dump that file to my default windows printer, how would...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
5
by: soup_nazi | last post by:
I want to remove duplicate entries within a text file. So if I had this within a text file... Applications/Diabetic Registry/ Applications/Diabetic Registry/ Applications/Diabetic Registry/...
2
by: tghamm | last post by:
Ok, so this is driving me mad. For some reason, regardless of the value of ev.hasmorepages, the printoducment1_printpage gets called twice for every page. So, I print 2 pages of data on one page,...
2
by: Chronos | last post by:
Hi all, Is it possible to print text file to a remote printer in C#? If so, can you tell me how? Thanks, Chronos
1
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...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
agi2029
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.