Rus*_*ell 140 python file head
我们有一个大的原始数据文件,我们想要修剪到指定的大小.我在.net c#中很有经验,但是想在python中做这件事来简化事情并且没有兴趣.
我如何在python中获取文本文件的前N行?使用的操作系统会对实施产生影响吗?
Joh*_*ooy 224
Python 2
with open("datafile") as myfile:
head = [next(myfile) for x in xrange(N)]
print head
Run Code Online (Sandbox Code Playgroud)
Python 3
with open("datafile") as myfile:
head = [next(myfile) for x in range(N)]
print(head)
Run Code Online (Sandbox Code Playgroud)
这是另一种方式(Python 2和3)
from itertools import islice
with open("datafile") as myfile:
head = list(islice(myfile, N))
print head
Run Code Online (Sandbox Code Playgroud)
gho*_*g74 18
N = 10
file = open("file.txt", "a")#the a opens it in append mode
for i in range(N):
line = file.next().strip()
print line
file.close()
Run Code Online (Sandbox Code Playgroud)
G M*_*G M 14
如果要快速读取第一行并且不关心性能,可以使用.readlines()哪个返回列表对象,然后对列表进行切片.
例如前5行:
with open("pathofmyfileandfileandname") as myfile:
firstNlines=myfile.readlines()[0:5] #put here the interval you want
Run Code Online (Sandbox Code Playgroud)
注意:整个文件是从性能的角度来看的,所以不是最好的,但它易于使用,编写速度快,易于记忆,所以如果你只想执行一些一次性计算是非常方便的
print firstNlines
Run Code Online (Sandbox Code Playgroud)
我所做的就是使用N行pandas.我认为性能不是最好的,但例如N=1000:
import pandas as pd
yourfile = pd.read('path/to/your/file.csv',nrows=1000)
Run Code Online (Sandbox Code Playgroud)
没有特定的方法来读取文件对象公开的行数.
我想最简单的方法是:
lines =[]
with open(file_name) as f:
lines.extend(f.readline() for i in xrange(N))
Run Code Online (Sandbox Code Playgroud)
执行此操作的两种最直观的方法是:
逐行和行break后迭代文件N。
next()使用方法times . 逐行迭代文件N。(这本质上只是最佳答案的不同语法。)
这是代码:
# Method 1:
with open("fileName", "r") as f:
counter = 0
for line in f:
print line
counter += 1
if counter == N: break
# Method 2:
with open("fileName", "r") as f:
for i in xrange(N):
line = f.next()
print line
Run Code Online (Sandbox Code Playgroud)
最重要的是,只要您不使用整个文件readlines()或enumerate将整个文件放入内存中,您就有很多选择。