with open("pathofmyfileandfileandname") as myfile:
firstNlines=myfile.readlines()[0:5] #put here the interval you want
Note: the whole file is read so is not the best from the performance point of view but it
is easy to use, fast to write and easy to remember so if you want just perform
some one-time calculation is very convenient
Python2
Python3
这是另一种方法(Python 2&3)
如果希望快速读取第一行,而不关心性能,可以使用
.readlines()
返回list对象,然后对列表进行切片。例如,对于前5行:
相关问题 更多 >
编程相关推荐