Pythonic way to ignore for loop control variable

前端 未结 9 1410
时光取名叫无心
时光取名叫无心 2020-12-03 11:22

A Python program I\'m writing is to read a set number of lines from the top of a file, and the program needs to preserve this header for future use. Currently, I\'m doing s

9条回答
  •  爱一瞬间的悲伤
    2020-12-03 12:08

    May be this:

    header_len = 4
    header = open("file.txt").readlines()[:header_len]
    

    But, it will be troublesome for long files.

提交回复
热议问题