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


Groups > comp.lang.python > #90249

Re: getting fieldnames from Dictreader before reading lines

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'resulting': 0.04; 'none:': 0.07; 'remaining': 0.07; 'subject:getting': 0.07; '*args,': 0.09; 'arguments': 0.09; 'lawrence': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'valueerror:': 0.09; 'language.': 0.14; "'r',": 0.16; "'w')": 0.16; '(below)': 0.16; '**kwds)': 0.16; 'dict': 0.16; 'dictionary.': 0.16; 'keyed': 0.16; 'last)': 0.16; 'omitted,': 0.16; 'operates': 0.16; 'optional': 0.16; 'parameter.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sequence,': 0.16; 'underlying': 0.16; 'valueerror': 0.16; 'elements': 0.16; 'language': 0.16; 'wrote:': 0.18; 'input': 0.22; 'this?': 0.23; 'header:User- Agent:1': 0.23; 'error': 0.23; 'file.': 0.24; 'order.': 0.26; 'pass': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'keys': 0.31; 'file': 0.32; 'regular': 0.32; 'url:python': 0.33; '(most': 0.33; 'reader': 0.33; 'skip:d 20': 0.34; 'subject:from': 0.34; 'except': 0.35; 'but': 0.35; 'keyword': 0.36; 'sequence': 0.36; 'doing': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'read': 0.60; 'new': 0.61; 'url:3': 0.61; 'first': 0.61; 'information': 0.63; 'skip:n 10': 0.64; 'our': 0.64; 'become': 0.64; 'more': 0.64; 'received:as9105.com': 0.84; 'received:dsl.as9105.com': 0.84; 'received:dynamic.dsl.as9105.com': 0.84; 'subject:before': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: getting fieldnames from Dictreader before reading lines
Date Sun, 10 May 2015 00:00:39 +0100
References <CALyJZZV8XFjbmiNRYxoAFuGHS2iix4knv-smn5adicVRAJ7ZCg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 80-44-199-135.dynamic.dsl.as9105.com
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
In-Reply-To <CALyJZZV8XFjbmiNRYxoAFuGHS2iix4knv-smn5adicVRAJ7ZCg@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.292.1431212465.12865.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1431212465 news.xs4all.nl 2832 [2001:888:2000:d::a6]:57302
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:90249

Show key headers only | View raw


On 09/05/2015 23:46, Vincent Davis 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​
>

 From https://docs.python.org/3/library/csv.html#module-csv

"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. The fieldnames parameter is a sequence whose 
elements are associated with the fields of the input data in order. 
These elements become the keys of the resulting dictionary. If the 
fieldnames parameter is omitted, the values in the first row of the 
csvfile will be used as the fieldnames. If the row read has more fields 
than the fieldnames sequence, the remaining data is added as a sequence 
keyed by the value of restkey. If the row read has fewer fields than the 
fieldnames sequence, the remaining keys take the value of the optional 
restval parameter. Any other optional or keyword arguments are passed to 
the underlying reader instance."

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: getting fieldnames from Dictreader before reading lines Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-10 00:00 +0100

csiph-web