Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'none:': 0.07; 'subject:getting': 0.07; 'try:': 0.09; 'valueerror:': 0.09; 'wrong,': 0.09; 'received:209.85.218': 0.14; "'r',": 0.16; "'w')": 0.16; '(below)': 0.16; 'last)': 0.16; 'valueerror': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'skip:f 30': 0.19; 'seems': 0.21; 'to:name :python-list@python.org': 0.22; 'this?': 0.23; 'error': 0.23; 'file.': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'file': 0.32; '(most': 0.33; 'reader': 0.33; 'skip:& 30': 0.33; 'skip:d 20': 0.34; 'subject:from': 0.34; 'received:209.85': 0.35; 'except': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'thanks': 0.36; 'should': 0.36; 'received:209': 0.37; 'skip:& 10': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; '8bit%:6': 0.40; 'how': 0.40; 'new': 0.61; 'skip:n 10': 0.64; '8bit%:10': 0.64; '2015': 0.84; 'subject:before': 0.84 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=ebywV+AiF+I9YKnnee5WSmAtVsvcEBpPkKQmeVpegFg=; b=PSz9kCUprEZQeks4Xnfz4LY/SxrrGvL9Ia3rZEV1iKtMSxZXswGKoRN8iRwYbC9M9Z 6UBgL6bFec7PdqIIc4CiY6xk5qd4WQNjniSrfjLg1khlDWb3VC42gGSDvkp/BQ4rDZ/t pBkBIoCKvKdwcDNdyKGzXc0e5AWsyOhgHMb6Y/1d3UNeileDL1mA4OvP8PG0Tuic0YYt aJ4+hWEEZM+uV2G28bhpKMJLNC91tdkDcgoqyzA4LebXMz2yNxy7lpeDiERAGwzWNzqp YdMfSBNim2xgNCjvhynWX4F4N1sMBo0lRuqTBdAdkv5KDOUbHuK4DzW2oTJoidnJiBmi GWqA== X-Gm-Message-State: ALoCoQnPNDJi4ZMIN1qosTrngrcJzaCZoBzypsHzUuZ3nvAuUgcZaStTelpSR7eg19/C5L3ff/gD X-Received: by 10.60.175.72 with SMTP id by8mr3256726oec.35.1431212515055; Sat, 09 May 2015 16:01:55 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Vincent Davis Date: Sat, 9 May 2015 17:01:34 -0600 Subject: Re: getting fieldnames from Dictreader before reading lines To: "python-list@python.org" Content-Type: multipart/alternative; boundary=047d7bd7648e8eeefd0515ae21bb X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 138 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431212523 news.xs4all.nl 2953 [2001:888:2000:d::a6]:58447 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 10910 X-Received-Body-CRC: 541164983 Xref: csiph.com comp.lang.python:90274 --047d7bd7648e8eeefd0515ae21bb Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 wrote: > I am reading a file with Dictreader and writing a new file. I want use th= e > 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 erro= r > (below) > > with open(readfile, 'r', encoding=3D'utf-8', errors=3D'ignore', newline= =3D'') as > csvread: > reader =3D DictReader(csvread) > with open(writefile, 'w') as csvwrite: > writer =3D DictWriter(csvwrite, delimiter=3D',', > fieldnames=3Dreader.fieldnames) > for line in reader: > pass > > ValueError Traceback (most recent call las= t) in ()----> 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 =3D next(self.reader) 97 exc= ept StopIteration: 98 pass > ValueError: I/O operation on closed file. > > > > Thanks > Vincent > =E2=80=8B Davis=E2=80=8B > > --047d7bd7648e8eeefd0515ae21bb Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Not sure what I was doing wrong, it seems to wo= rk now.

Vincent Davis
720-301-3003

On Sat, May 9, 2015 at 4:46 PM, Vincent Davi= s <vincent@vincentdavis.net> wrote:
I am reading a file with Dict= reader 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=3D'utf-8'= , errors=3D'ignore', newline=3D'') as csvread:
=C2=A0 =C2=A0 reader =3D DictReader(csvread)
=C2=A0 =C2=A0 with open(writefile, 'w') a= s csvwrite:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 w= riter =3D DictWriter(csvwrite, delimiter=3D',', fieldnames=3Dreader= .fieldnames)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = for line in reader:
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 pass

ValueError                                Traceback (mos=
t 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 =3D next(self.reader)
     97             except StopIteration:
     98                 pass

ValueError: I/O operation on clos=
ed file.


Thanks
Vincent
=E2= =80=8B Davis=E2=80=8B


--047d7bd7648e8eeefd0515ae21bb--