'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.

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

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


Solution 1:[1]

You could just break after 10 lines.

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

Solution 2:[2]

Use 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:

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

Solution 3:[3]

Adrien El Zein's answer is enough for your question. 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. 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.

Solution 4:[4]

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

#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]

Solution 5:[5]

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  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adrien El Zein
Solution 2
Solution 3
Solution 4
Solution 5 codeholic24