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


Groups > comp.lang.python > #90273

getting fieldnames from Dictreader before reading lines

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 <vincent@vincentdavis.com>
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 <vincent@vincentdavis.net>
Date Sat, 9 May 2015 16:46:18 -0600
Subject getting fieldnames from Dictreader before reading lines
To "python-list@python.org" <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 <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.290.1431211607.12865.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

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​

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


Thread

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

csiph-web