[英]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.