top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python: What is dictreader() and reader() function call while reading from a csv file?

+2 votes
2,495 views
Python: What is dictreader() and reader() function call while reading from a csv file?
posted Dec 17, 2013 by Satyabrata Mahapatra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

class csv.DictReader(csvfile, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)

Create an object which operates like a regular reader but maps the information read into a dict whose keys are given by the optional fieldnames parameter. I hope this helps.

answer Dec 17, 2013 by Anderson
+1 vote

csv.reader(csvfile)
Creates a reader object which can parse the given file, returning a sequence of values for each line of the file. The csvfile can be any iterable object.

csv.DictReader(csvfile[, fieldnames])
Creates a DictReader object which can parse the given file, returning a dictionary of values for each line of the file. The dictionary keys are typically the first line of the file. You can, optionally, provide the field names if they are not the first line of the file. The csvfile can be any iterable object.

Hope this helps.

answer Dec 17, 2013 by Salil Agrawal
Similar Questions
+1 vote

I've a huge csv file and I want to read stuff from it again and again. Is it useful to pickle it and keep and then unpickle it whenever I need to use that data? Is it faster that accessing that file simply by opening it again and again? Please explain?

+2 votes

Is there a better way to do that:

def Read_CSV_File(filename):
 file = open(filename, "r")
 reader = csv.DictReader(file)
 line = 1
 for row in reader:
 if line < 6:
 reader.next()
 line++
# process the CSV
0 votes

The problem is that some of the fields contain commas, but they are inside double quotes.

Example:

sort -t, -k1,1 -k3,3 -k2,2 SomeFile.csv > OutputFile.csv

A line could look something like this:
This is the first field,"This is, well, the second field",The third field could look like this

That line has three fields:
1: This is the first field
2: "This is, well, the second field"
3: The third field could look like this

But sort consider it to have five fields:
1: This is the first field
2: "This is
3: well
4: the second field
5: The third field could look like this

How would you solve this?

+1 vote

I just wanted to see the best way to securely accomplish this task. When we want to update a DB we upload to a writable directory instead of writing it directly to MySQL, I don't like having writable directories if possible.
Is there a right or better way to accomplish this?

...