Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'none:': 0.07; 'subject:getting': 0.07; 'try:': 0.09; 'valueerror:': 0.09; 'received:209.85.218': 0.14; "'r',": 0.16; "'w')": 0.16; '(below)': 0.16; 'last)': 0.16; 'valueerror': 0.16; 'skip:f 30': 0.19; 'to:name:python-list@python.org': 0.22; 'this?': 0.23; 'error': 0.23; 'file.': 0.24; 'pass': 0.26; '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; 'recent': 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; '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:from:date:message-id:subject:to :content-type; bh=h6XYjNPHPhGKlTOBadPibs9k+Pqnu2+/Gx0dXnGpecw=; b=ecXn0wmih1xdoucrcHFp4dFqJqgFIDCiLmE1o3QY7c5CiQI2JOg1s3MueHOKYZpcKd nXr8R8qR8WgcERl8Rqcd3ay3L72k9hnnSIonG9DcSpN9ikavup+QfsyckTR8LcUBZaa8 J4/vmCdh/siaxm8tuF86veDmSJYKjky071UZZ/DLHdwdustQYS3VIpFd9Zcs42PLWmLp 2pH3lOitoNVLnfcVNx1P9wfHU56BupvrLY51x3p+j3UzPC6Hhv0Lre8f3Cqt151vn8pO UEu0WRWtlks8kO+ImtwuZ5p77CZl6iACn+BbEAa9dLUNqtpFVYXBFJY6527p00oJzIHd zkKg== X-Gm-Message-State: ALoCoQltVjIeU2QKXVvEKTs1iGcbM9jrsHqw3VksROQGnN4HA5htG/UHlv3VbRvnlDTFRJuun2e/ X-Received: by 10.202.73.81 with SMTP id w78mr2969362oia.89.1431211598284; Sat, 09 May 2015 15:46:38 -0700 (PDT) MIME-Version: 1.0 From: Vincent Davis Date: Sat, 9 May 2015 16:46:18 -0600 Subject: getting fieldnames from Dictreader before reading lines To: "python-list@python.org" Content-Type: multipart/alternative; boundary=001a113db194ea1fee0515adea65 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: 122 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431211607 news.xs4all.nl 2869 [2001:888:2000:d::a6]:49660 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90273 --001a113db194ea1fee0515adea65 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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=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 last) 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 except StopIteration: 98 pass ValueError: I/O operation on closed file. Thanks Vincent =E2=80=8B Davis=E2=80=8B --001a113db194ea1fee0515adea65 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I am reading a file with Dictreader and writing= a new file. I want use the fieldnames in the Dictwriter from the reader. S= ee 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') as csvwrite:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 writer =3D DictWr= iter(csvwrite, delimiter=3D',', fieldnames=3Dreader.fieldnames)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 for line in read= er:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 pass

ValueError                                Traceback (most r=
ecent call last)
<ipython-input-13-0dac622b=
b8a9> in <mod=
ule>()
----> 1 reader.fieldnames()

/Users/vmd/anaconda/envs/py34/lib/python3.4/csv.py =
in fieldnames(self)
     94         if self._fieldnames is Non=
e:
     95             try:
---> 96                 self._fieldnames =3D next(self.reader=
)
     97             except =
StopIteration:
     98                 pass

ValueError: I/O operat=
ion on closed file.
=


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

--001a113db194ea1fee0515adea65--