Assuming that with "data" you refer to the rows, taking into account that csv.reader
returns an iterator you can not perform a slicing on it directly, you would have to create a list or tuple with all the lines first:
rows = list(entrada)
primeras_siete = rows[:7]
Now, this implies loading the entire file in memory which you should avoid if it is not necessary. If you want to obtain the first x rows or a fragment of it efficiently, the solution is to consume the generator manually called next
x times (checking that it has not been exhausted) or use enumerate
to obtain only those rows that we interesting:
import csv
with open('val.csv') as csvarchivo:
entrada = csv.reader(csvarchivo)
for index, row in enumerate(entrada):
print(row)
if index == 6:
break
Modifying the conditional can get only rows with a certain index, a range, etc.
However, a simpler option than all this is to use itertools.islice
that does the above for you:
import csv
from itertools import islice
with open('val.csv') as csvarchivo:
entrada = csv.reader(csvarchivo)
rows = list(islice(entrada, 7))
Where rows
is a list that will contain the first 7 rows of the file (or those containing if it has less than 7).
To only print them (if it has any utility by itself) just:
with open('val.csv') as csvarchivo:
entrada = csv.reader(csvarchivo)
for row in islice(entrada, 7):
print(row)
itertools.islice
allows you to perform slicing on an iterator (any iterable actually) as well as slicing on lists or tuples, with the exception of not allowing negative indexing (this implies knowing the length of the iterator , which implies consuming it previously), returns another iterator on the concrete fragment of the iterable:
-
islice(entrada, 10, 21)
- > from line 10 to 20 (indexes start at 0)
-
islice(entrada, 10, 21, 2)
- > from line 10 to 20 but one if not (even rows).
Note that iterate over an iterator by the method that "consumes" it , that is, if you redo rows = list(islice(entrada, 5))
after rows = list(islice(entrada, 7))
you do not get the first 5 lines of the file , you get the following 5 lines to the seventh (which was where% was rows = list(islice(entrada, 7))
).