Python CSV Error
<p>Hello all I keep getting this error while making a small program to sort large CSV files out, below is my code and error, what am I doing wrong?</p>

<pre><code>if selection:
        for stuff in stuffs:

            try:
                textFile = open("output.txt",'w')
                mycsv = csv.reader(open(stuff))
                d_reader = csv.DictReader(mycsv)
                headers = d_reader.fieldnames &lt;-- Error happens here
                if selection in headers:
                    placeInList = headers.index(selection)

                    #placeInList = selection.index(selection)
                    for selection in tqdm(mycsv, desc='Extracting column values...', leave = True):
                        textFile.write(str(selection[int(placeInList)])+'\n')
                    print 'Done!'
                    textFile.close()
                    sys.exit()
            except IOError:
                print 'No CSV file present in directory'
                sys.exit()
    else:
        sys.exit()
</code></pre>

<p>And the error:</p>

<pre><code>Traceback (most recent call last): 
File "postcodeExtractor.py", line 27, in &lt;module&gt; headers = d_reader.fieldnames 
File "C:\Python27\lib\csv.py", line 90, in fieldnames self._fieldnames = self.reader.next() 
TypeError: expected string or Unicode object, list found
</code></pre>

<p>instead of </p>

<pre><code>mycsv = csv.reader(open(stuff))
d_reader = csv.DictReader(mycsv)
</code></pre>

<p>you want </p>

<pre><code>d_reader = csv.DictReader(open(stuff))
</code></pre>

<p>the first line is the problem.</p>

python python-2.7 csv