Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

similarity - How do I extract similar line between two text file in Python

I would like to extract only the similar line between 2 files. Please can someone help me?
I have two text files

file1
Thailand officially the kingdom of Thailand and formerly known as Siam is a country in Southeast Asia located at the centre of the Indochinese peninsula it is composed of seventy six provinces and covers an area of five hundred thirty thousand one hundred twenty square kilometres and a population of over sixty six million people Thailand is the world's fiftieth largest country by land area and the twenty second most populous country in the world

file2
Thailand,[a] officially the Kingdom of Thailand and formerly known as Siam,[b] is a country in Southeast Asia. Located at the centre of the Indochinese Peninsula, it is composed of 76 provinces, and covers an area of 513,120 square kilometres (198,120 sq mi), and a population of over 66 million people.[4] Thailand is the world's 50th-largest country by land area, and the 22nd-most-populous country in the world. The capital and largest city is Bangkok, a special administrative area.


Actually, file2 is longer than this which is downloaded from Wikipedia(about the Thailand) and I want to extract only the same line from file1.

That is my code which extract only the exact same line if there has any change word like (sixteen and 16) that doesn't count as the same line and do not extract.
org_content = open('file2.txt').read()
user_file = open('file1.txt')
for line in user_file:
    if line in org_content:
        with open('file3.txt', 'w') as out:
            out.writelines(line)
    else:
        with open('file3.txt', 'w') as out:
            out.writelines("not found")

and output file3 got "not found" but what I want is extract the similar line even those are not 100% exactly the same.

question from:https://stackoverflow.com/questions/65872261/how-do-i-extract-similar-line-between-two-text-file-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...