regex 比较单个文本文件中的行

inn6fuwd  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(125)

我有一个文本文件,它由类似的行组成,很少有半个类似于文本文件中的其他行。
Input.txt

I would like to play: Volleyball
I would like to play: Volleyball
I would like to play: TableTennis
I would like to play: Baseball
I do not know how to play: Volleyball
She would like to play: TableTennis
I want to learn how to play: Baseball
They like to play: all the three

从输入文件中,我想删除重复的行,如图所示

I would like to play: Volleyball
I would like to play: TableTennis
I would like to play: Baseball
I do not know how to play: Volleyball
She would like to play: TableTennis
I want to learn how to play: Baseball
They like to play: all three

从输入文件中,我想删除重复的行,如图所示

I would like to play: Volleyball
I would like to play: TableTennis
I would like to play: Baseball
I do not know how to play: Volleyball
She would like to play: TableTennis
I want to learn how to play: Baseball
They like to play: all three

下一步:

I would like to play
They like to play

输出文件的简要说明我想玩的语句涵盖了许多不同的运动,所以我想打印出来。他们喜欢玩的最后一行是不同的情况,所以我也想打印那一行。(我们将这些结果写入.csv格式,并在不同的列中打印覆盖最大数量的运动以及所有独特运动的语句如何?)
注意:我不想打印我不知道怎么玩:排球她想玩:乒乓球我想学习如何玩:棒球
因为已经有三项运动
我对如何将同一文本文件中的一行与另一行进行比较感到困惑。

x6h2sr28

x6h2sr281#

您可以按照以下步骤操作:

with open('Input.txt') as f:
    content = f.readlines()
import pandas as pd
content=pd.unique(content).tolist()

with open('Input.txt') as f:
    content = f.readlines()
result = []
for line in content:
    if line not in result:
        result.append(line)
xu3bshqb

xu3bshqb2#

这很简单,在你的'.py'文件中这样做:

"""Simple Solution To Your Problem!"""

# Opening The Input File- `input.txt`
f = open('input.txt', encoding='utf-8', mode='w+')
new_file = '\
I would like to play: Volleyball\n\
I would like to play: Volleyball\n\
I do not know how to play: Volleyball\n\
I would like to play: Baseball\n\
I want to learn how to play: Volleyball'
f.write(new_file)
del f  # To Read The File Again

# Next, Printing Lines 1, 3, 4
with open('input.txt', encoding='utf-8', mode='r') as f:
lines = f.readlines()
wanted_lines = [0, 3, 4]
for each_line in wanted_lines:
    print(lines[each_line])
del f  # Just To Save Some Memory

相关问题