Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90274 > unrolled thread

Re: getting fieldnames from Dictreader before reading lines

Started byVincent Davis <vincent@vincentdavis.net>
First post2015-05-09 17:01 -0600
Last post2015-05-09 17:01 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: getting fieldnames from Dictreader before reading lines Vincent Davis <vincent@vincentdavis.net> - 2015-05-09 17:01 -0600

#90274 — Re: getting fieldnames from Dictreader before reading lines

FromVincent Davis <vincent@vincentdavis.net>
Date2015-05-09 17:01 -0600
SubjectRe: getting fieldnames from Dictreader before reading lines
Message-ID<mailman.293.1431212523.12865.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Not sure what I was doing wrong, it seems to work now.

Vincent Davis
720-301-3003

On Sat, May 9, 2015 at 4:46 PM, Vincent Davis <vincent@vincentdavis.net>
wrote:

> I am reading a file with Dictreader and writing a new file. I want use the
> fieldnames in the Dictwriter from the reader. See below How should I be
> doing this?
>
> See how I am using reader.fieldnames in the the Dictwriter. I get an error
> (below)
>
> with open(readfile, 'r', encoding='utf-8', errors='ignore', newline='') as
> csvread:
>     reader = DictReader(csvread)
>     with open(writefile, 'w') as csvwrite:
>         writer = DictWriter(csvwrite, delimiter=',',
> fieldnames=reader.fieldnames)
>         for line in reader:
>             pass
>
> ValueError                                Traceback (most recent call last)<ipython-input-13-0dac622bb8a9> in <module>()----> 1 reader.fieldnames()
> /Users/vmd/anaconda/envs/py34/lib/python3.4/csv.py in fieldnames(self)     94         if self._fieldnames is None:     95             try:---> 96                 self._fieldnames = next(self.reader)     97             except StopIteration:     98                 pass
> ValueError: I/O operation on closed file.
>
>
>
> Thanks
> Vincent
> ​ Davis​
>
>

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web