简体   繁体   中英

How to compare lines from a text file in Python?

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?

This will give you a list of all the lines:

with open(path_to_file) as f:
    list_of_lines = f.readlines()
for line in f1:
    for line_aux in f2:
        compare the two lines

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM