How to compare lines from a text file in Python?

python file

108 观看


21 作者的声誉

hope this is not a trivial question since I'm new to Python. I have a text file, and I need to compare the first line with all the following ones, than the second line with all the following ones, etc. If it was a list I'd just make two for loops, but I don't know how to start reading a file from the line after the one that needs to be compared. Could someone help me?

作者: SnowyNe 的来源 发布者: 2017 年 12 月 27 日

回应 (3)


1208 作者的声誉

This will give you a list of all the lines:

with open(path_to_file) as f:
    list_of_lines = f.readlines()
作者: it's-yer-boy-chet 发布者: 27.12.2017 05:08


46 作者的声誉

for line in f1:
    for line_aux in f2:
        compare the two lines
作者: PedroNeves 发布者: 27.12.2017 05:08


1137 作者的声誉


You can get a list with each line as an element using a readlines() function.

with open(file_name) as file:
    lines_list = f.readlines()
作者: Andrej Hafner 发布者: 27.12.2017 05:10