Reading specific lines only

后端 未结 28 2119
天命终不由人
天命终不由人 2020-11-22 05:08

I\'m using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?

Thanks

28条回答
  •  清歌不尽
    2020-11-22 05:33

    File objects have a .readlines() method which will give you a list of the contents of the file, one line per list item. After that, you can just use normal list slicing techniques.

    http://docs.python.org/library/stdtypes.html#file.readlines

提交回复
热议问题