简体   繁体   English

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

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

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'])

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.itemgetter to 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))

You could just break after 10 lines. 你可以在10行后break

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

Adrien El Zein's answer is enough for your question. Adrien El Zein的回答足以满足您的提问。 However, if you think it's slightly confusing (I don't think so): 但是,如果你认为它有点令人困惑(我不这么认为):

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 i to counter . 我所做的就是将变量i重命名为counter Also, for an alternative loop: 另外,对于替代循环:

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-else loop using Python 3.4.3 (not sure which version you have) and can tell you that it works properly. 我尝试使用Python 3.4.3测试while-else循环(不确定你有哪个版本)并且可以告诉你它正常工作。

May be this could help you也许这可以帮助你

Code:代码:

count = 0
rg = 10
with open('/content/gdrive/MyDrive/Colab Notebooks/employee.csv','r') as csvdt:
  csv_rd = csv.reader(csvdt)
  for j in  csv_rd:
    if count < rg:
      print(j)
      count = count + 1  

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何从 csv 文件中仅打印正确的行 - Python: how to print only the lines that are true from csv file 我试图在 python 中只打印文件的前 5 行 - I am trying to print only the first 5 lines of a file in python 如何使用 Python 仅打印文本文件中字符串的第一个实例? - How do I print only the first instance of a string in a text file using Python? 如何使用python将所需的行从一个csv文件复制到另一个csv文件? - How do I copy only the required rows from one csv file to other csv file using python? 如何从使用熊猫上传的大型CSV文件中仅打印出某些行 - How do I print out only certain rows from a large CSV file uploaded using pandas 如何使用python将行放入CSV列表中 - How do I put lines into a list from CSV using python 如何使用Python从包含特定单词的文件中打印行数? - How do I print the number of lines from a File that contains a specific word using Python? 如何从python中的不同输入打印到CSV文件 - how do i print to a a CSV file from different inputs in python 如何从 python 中的 CSV 文件打印特定字段? - How do I print a particular field from a CSV file in python? 如何搜索关键字并打印文件,只打印关键字中的行 - How do I search a keyword and print a file with only print the lines from the keyword on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM