Copy the last three lines of a text file in python?

前端 未结 5 1783
故里飘歌
故里飘歌 2020-12-17 03:40

I\'m new to python and the way it handles variables and arrays of variables in lists is quite alien to me. I would normally read a text file into a vector and then copy the

5条回答
  •  星月不相逢
    2020-12-17 03:43

    This might be a little clearer if you do not know python syntax.

    lst_lines = lines.split()

    This will create a list containing all the lines in the text file.

    Then for the last line you can do:

    last = lst_lines[-1] secondLAst = lst_lines[-2] etc... list and string indexes can be reached from the end with the '-'.

    or you can loop through them and print specific ones using:

    start = start line, stop = where to end, step = what to increment by.

    for i in range(start, stop-1, step): string = lst_lines[i]

    then just write them to a file.

提交回复
热议问题