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'])
解决方案
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