我们有一个很大的原始数据文件,我们想把它修剪成指定的大小。

如何在python中获取文本文件的前N行?所使用的操作系统对实现有任何影响吗?


当前回答

对于前5行,简单地做:

N=5
with open("data_file", "r") as file:
    for i in range(N):
       print file.next()

其他回答

这对我很有效

f = open("history_export.csv", "r")
line= 5
for x in range(line):
    a = f.readline()
    print(a)

fname = input("Enter file name: ")
num_lines = 0

with open(fname, 'r') as f: #lines count
    for line in f:
        num_lines += 1

num_lines_input = int (input("Enter line numbers: "))

if num_lines_input <= num_lines:
    f = open(fname, "r")
    for x in range(num_lines_input):
        a = f.readline()
        print(a)

else:
    f = open(fname, "r")
    for x in range(num_lines_input):
        a = f.readline()
        print(a)
        print("Don't have", num_lines_input, " lines print as much as you can")


print("Total lines in the text",num_lines)

对于前5行,简单地做:

N=5
with open("data_file", "r") as file:
    for i in range(N):
       print file.next()

我所做的就是用熊猫形来称呼N行。我认为性能不是最好的,但是举个例子,如果N=1000:

import pandas as pd
yourfile = pd.read_csv('path/to/your/file.csv',nrows=1000)

这适用于Python 2和3:

from itertools import islice

with open('/tmp/filename.txt') as inf:
    for line in islice(inf, N, N+M):
        print(line)