Reading the first n lines of CSV in a dictionary
I have a CSV file that I would like to read into a dictionary for insertion into a MongoDB collection titled projects.
I did it with the following:
with open('opendata_projects.csv') as f:
records = csv.DictReader(f)
projects.insert(records)
However, I found that my poor sandbox account cannot store all the data. In turn, I would like to read in the first n lines so that I can play with the data and get used to working with MongoDB.
I first checked the docs for the function csv.DictReader
:
class csv.DictReader (csvfile, fieldnames = None, restkey = None, restval = None, dialect = 'excel', * args, ** kwds)
But the function doesn't seem to allow me to enter the number of lines I need as a parameter.
So I tried to do it by writing the following code:
with open('opendata_projects.csv') as f:
records = csv.DictReader(f)
for i in records:
if i <= 100:
projects.insert(i)
This was followed by an error:
TypeError: unorderable types: dict() <= int()
This prompted me to look further into the dictionaries, and I found they were disordered. However, it seems that the example from the Python csv docs suggests that I can iterate with csv.DictReader
:
with open('names.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['first_name'], row['last_name'])
Is there a way to accomplish what I would like to do using these functions?
source to share
You can use itertools.islice
for example
import csv, itertools
with open('names.csv') as csvfile:
for row in itertools.islice(csv.DictReader(csvfile), 100):
print(row['first_name'], row['last_name'])
islice
will create an iterator from the iterable you traverse, and it will let you iterate to the limit you pass as the second parameter.
Also, if you want to count yourself, you can use enumerate
like this
for index, row in enumerate(csv.DictReader(csvfile)):
if index >= 100:
break
print(row['first_name'], row['last_name'])
source to share