如何使用 Python 仅打印 csv 文件中的前 10 行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37088512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:48:57  来源:igfitidea点击:

How do I print only the first 10 lines from a csv file using Python?

pythonpython-3.xcsv

提问by Adorabubble

I'm new to Python and I'm wanting to print only the first 10 lines of a huge csv file.

我是 Python 新手,我只想打印一个巨大的 csv 文件的前 10 行。

Here's my code so far that prints all of the lines in the csv file

到目前为止,这是我的代码,它打印了 csv 文件中的所有行

import csv
with open('titanic.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row['survived'], row['pclass'], row['name'], row['sex'], row['age'])

回答by Jon Clements

Use itertools.islice:

使用itertools.islice

import csv
from itertools import islice

with open('titanic.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in islice(reader, 10): # first 10 only
        print(row['survived'], row['pclass'], row['name'], row['sex'], row['age'])

While you're at it, you can also make use of operator.itemgetterto make the column getting a bit easier:

当您使用它时,您还可以使用operator.itemgetter使列变得更容易一些:

import csv
from itertools import islice
from operator import itemgetter

get_columns = itemgetter('survived', 'pclass', 'name', 'sex', 'age')

with open('titanic.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in islice(reader, 10): # first 10 only
        print(*get_columns(row))

回答by Adrien El Zein

You could just breakafter 10 lines.

你可以break在 10 行之后。

import csv
with open('titanic.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for i,row in enumerate(reader):
        print(row['survived'], row['pclass'], row['name'], row['sex'], row['age'])
        if(i >= 9):
            break

回答by Adorabubble

Adrien El Zein's answer is enough for your question. However, if you think it's slightly confusing (I don't think so):

Adrien El Zein 的回答足以解决您的问题。但是,如果您认为这有点令人困惑(我不这么认为):

import csv
counter = 0

with open('titanic.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in enumerate(reader):
       print(row['survived'], row['pclass'], row['name'], row['sex'], row['age'])
       counter += 1
       if counter >= 9:
           break

All I did was rename the variable ito counter. Also, for an alternative loop:

我所做的只是将变量重命名icounter. 另外,对于替代循环:

import csv
counter = 0

with open('titanic.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in enumerate(reader):
       print(row['survived'], row['pclass'], row['name'], row['sex'], row['age'])
       while counter < 10:
           counter += 1
       else:
           break

I tried and tested the while-elseloop using Python 3.4.3 (not sure which version you have) and can tell you that it works properly.

while-else使用 Python 3.4.3(不确定您使用的是哪个版本)尝试并测试了循环,并且可以告诉您它可以正常工作。

回答by Madhusoodanan K madhavan

To get top N lines from a csv file, with select fields

从 csv 文件中获取前 N 行,并带有选择字段

#for python 3
import csv
from operator import itemgetter
N=11 # to get 10 rows need use 10+1=11
fname='titanic.csv'
get_columns=itemgetter('survived', 'pclass', 'name', 'sex', 'age')

with open(fname,'r') as csvfile:
    reader = csv.DictReader(csvfile.readlines()[0:N])
    [print(*get_columns(row)) for row in reader]

     # or for all fields : use [print(row)) for row in reader]