473,378 Members | 1,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

find repetitions in text file and delete repeated lines

2
Hello

I am new to python. I have merged two text files and I am trying to write a python script to find repetitions in these 2 files and delete the repeated lines. Can anyone give me some help on that? Thanks.
Jul 15 '10 #1
2 5478
bvdet
2,851 Expert Mod 2GB
Let's assume you have read in the file as a list of lines and assigned it to the identifier lineList. This can be done with file object method readlines(). You can remove the duplicate lines with a for loop and the in operator.
Expand|Select|Wrap|Line Numbers
  1. output = []
  2. for item in lineList:
  3.     if item not in output:
  4.         output.append(item)
This list comprehension works in Python 2.3:
Expand|Select|Wrap|Line Numbers
  1. [s for s in lineList if s not in locals()['_[1]'].__self__]
If order is not important, you can use set() or dict(). Example using dict():
Expand|Select|Wrap|Line Numbers
  1. >>> lineList = ["A", "B", "C", "C"]
  2. >>> dict.fromkeys(lineList).keys()
  3. ['A', 'C', 'B']
  4. >>> 
Example using set():
Expand|Select|Wrap|Line Numbers
  1. >>> lineList = ["A", "B", "C", "C"]
  2. >>> list(set(lineList))
  3. ['A', 'C', 'B']
  4. >>> 
Jul 15 '10 #2
Mehr21
2
Thank you so much for the your help.
Oct 17 '10 #3

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

Similar topics

4
by: MJ | last post by:
Currently i am using the below coding to create a text file and write something on it. Dim wfile As New System.IO.StreamWriter("c:\test.txt" wfile.WriteLine("Hello" & vbcrlf & "Are" & vbcrlf &...
3
by: kittykat | last post by:
Hi, I was wondering if you could help me. I am writing a program in C++, and the problem is, i have very limited experience in this language. I would like my user to enter a specific pattern, and...
4
by: Paul | last post by:
Hello Everybody, Being new to VBA for Access, I have a question about inputing a text file into access. The text file has 23 lines per file and needs to go into 1 record in a table. The 1st...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
1
by: JenHu | last post by:
Hi all, I want to create a text file to write lines, but it gives me an error on Dim sw As New StreamWriter(CreatePath & Efilename) : System.IO.IOException: The process cannot access the file...
4
by: anne001 | last post by:
Hi For a class, students are going to run an experiment on line. Each time a subject runs, his/her data is appended to one giant text file. Their own data set will be just one line starting with...
1
by: John | last post by:
I have a process that reads a text file then uploads the data into a database table. The text file has 10 lines at the end of the file that are blank BUT it appears that the enter key or space bar...
1
by: Osoccer | last post by:
...to a different folder and in the relocated file concatenates all of the lines in one long string with a space between each line element. Here is a fuller statement of the problem: I need a...
13
by: JJ | last post by:
I have a need to input a large tab delimited text file, which I will parse to check it has the expected columns, before allowing the user to submit it to the database. The user may paste the file...
4
by: nxstar | last post by:
Hi I am confused about how to go about writing this program in perl. I have a text file with data in columns 8 and 9. I need to extract the data value from column 8 from the line being read and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.