Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #62429
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ikorot01@gmail.com> |
| 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; 'python.': 0.02; 'else:': 0.03; 'broken': 0.04; 'args': 0.07; 'none,': 0.07; 'parser': 0.07; 'skip:` 10': 0.07; 'subject:file': 0.07; 'sys': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'attributes': 0.09; 'conventions.': 0.09; 'filename': 0.09; 'none)': 0.09; 'underscore': 0.09; 'variant': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'bug': 0.12; '"r")': 0.16; '*args):': 0.16; 'be:': 0.16; 'bug,': 0.16; 'csv': 0.16; 'exception:': 0.16; 'filename):': 0.16; 'filename,': 0.16; 'ignoring': 0.16; 'igor': 0.16; 'made-up': 0.16; 'naming': 0.16; 'open()': 0.16; 'skip:': 0.16; 'skipped': 0.16; 'stringio': 0.16; 'subject:Reading': 0.16; 'suggestion.': 0.16; 'to:addr:web.de': 0.16; 'url:pep-0008': 0.16; 'url:peps': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'discussion': 0.18; 'have:': 0.19; "python's": 0.19; 'skip:f 30': 0.19; 'thu,': 0.19; '>>>': 0.22; 'import': 0.22; 'hack': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'example.': 0.24; 'replace': 0.24; 'skip': 0.24; 'skip:{ 20': 0.24; 'url:dev': 0.24; 'header': 0.24; 'helpful': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'script': 0.25; 'skip:" 30': 0.26; 'tracker': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'function': 0.29; 'url:bugs': 0.29; 'am,': 0.29; "doesn't": 0.30; 'dec': 0.30; 'skip:@ 10': 0.30; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'comments': 0.31; 'easier': 0.31; 'lines': 0.31; 'class': 0.32; 'supposed': 0.32; 'another': 0.32; 'url:python': 0.33; 'bugs': 0.33; 'reader': 0.33; 'actual': 0.34; 'comment': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; "can't": 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'belong': 0.36; 'yield': 0.36; 'url:listinfo': 0.36; 'doing': 0.36; 'method': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'implement': 0.38; 'thank': 0.38; 'rather': 0.38; 'does': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'company': 0.60; 'introduced': 0.61; 'mentioned': 0.61; 'real': 0.63; 'skip:n 10': 0.64; 'fashion': 0.64; 'places': 0.64; 'great': 0.65; 'here': 0.66; 'introduce': 0.78; "'1'}": 0.84; "'2'}": 0.84; 'mock': 0.84; 'otten': 0.84; 'peter,': 0.84; 'convinced': 0.93; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=xo2ya/ZQeQzXgnkqvMPZyLLd4jQpFxiwY+F+PYlr+XA=; b=IfrXqXzqdQOI0u6KwpMgIyFVgVYfYu0SVxGq3gRwyCbd15s0I6oZ6YqxQ2RRS+ruke nsUfoSIseCQ9jKAJy07P7MgmVCO0QFkX9RySrUE69QJUoNZsab3IEfp2YNRydL5RgEcs LNRnIhPVHGLTKoa4HcxAEnYSYKk7/ADHrJs7lfPoGr1F8gChSBNUWvVX5aiT6cqKODWu w030BnV5ewWmW+4VfStC8KyJfG6zCyOEdHCLqQ0j8l3ztacuwJrWNRoAgQw9i6lrtitw 3bmFZtc8lYe+4/noAe8cUuqt0H0Jcz1xed94vo72UvFFpzIfAtExTzOowN/EMc7w+t4H fNJw== |
| MIME-Version | 1.0 |
| X-Received | by 10.182.250.200 with SMTP id ze8mr75828obc.72.1387528030880; Fri, 20 Dec 2013 00:27:10 -0800 (PST) |
| In-Reply-To | <l8uibv$t2k$1@ger.gmane.org> |
| References | <CA+FnnTwpiq=Z6tswub9vab2wxzzQycwyL2jNCgK2RTQuhGrrMQ@mail.gmail.com> <l8p1f5$gk0$1@ger.gmane.org> <l8p3gj$3gb$1@ger.gmane.org> <CA+FnnTw+Qt-g8+aDeMi2nDENsWDDVzW6AZBLJzcCJtvEKjrRZg@mail.gmail.com> <l8pa8t$mmi$1@ger.gmane.org> <CA+FnnTy_EDipp_CTwq3ePa8KfgKOUF2YqyXo9DpReZGP8aS32g@mail.gmail.com> <l8uibv$t2k$1@ger.gmane.org> |
| Date | Fri, 20 Dec 2013 00:27:10 -0800 |
| Subject | Re: Reading csv file |
| From | Igor Korot <ikorot01@gmail.com> |
| To | Peter Otten <__peter__@web.de> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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.4431.1387528034.18130.python-list@python.org> (permalink) |
| Lines | 215 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1387528034 news.xs4all.nl 2926 [2001:888:2000:d::a6]:44584 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:62429 |
Show key headers only | View raw
Thank you, Peter.
About OOP: company policy, can't help it.
They say it's easier to maintain and code.
But it works now.
On Thu, Dec 19, 2013 at 2:39 AM, Peter Otten <__peter__@web.de> wrote:
> Igor Korot wrote:
>
>> Hi, Peter,
>> Thank you for the great suggestion.
>>
>> I tried to implement you code but failed.
>>
>> Here's what I have:
>>
>> class FileReader:
>> def __init__(self, filename, isSkip):
>> self.path = filename
>> self.isSkip = isSkip
>>
>> @contextmanager
>> def open(*args):
>
> Selfless OO? Not in Python.
>
>> from StringIO import StringIO
>> lines = range(10)
>> if self.isSkip:
>> lines[0] = "skipped"
>> lines[6] = "field1-from-line6,field2-from-line6"
>> else:
>> lines[0] = "field1-from-line1,field2-from-line1"
>> yield StringIO("\r\n".join(map(str, lines)))
>>
>> def is_arbitrary_text(self,fieldnames):
>> return "skipped" in fieldnames
>>
>> def readData(self):
>> with self.open(self.path, "r") as f:
>> reader = csv.DictReader(f)
>> if self.is_arbitrary_text(reader.fieldnames):
>> for _ in range(5):
>> next(reader, None)
>> reader._fieldnames = None
>
> Here you introduced another bug, ignoring my helpful comments.
>
>>> reader._fieldnames = None # underscore necessary,
>>> # fieldnames setter doesn't work
>>> reader.fieldnames # used for its side-effect
>
>> for row in reader:
>> print row
>>
>> Unfortunately this does not work as "def open()" does not belong to my
>> class and if I comment the "@contextmanager" line
>> I will get an exception: "AttributeError: __exit__"
>>
>> Any idea what to do?
>
> Keeping comments is not an option? But please read and try to understand the
> comments before you excise them ;)
>
> As I mentioned in the comment to the open() function you are not supposed to
> use it as you have real data -- use Python's built-in open() function.
> Anyway, if you insist on doing everything the OO-way, at least add a self in
> all the right places and don't introduce bugs that could be avoided with
> copy-and-paste.
>
> A working script with mock data and following the OO fashion would be:
>
> $ cat csv_skip_header_oo.py
> import csv
> from contextlib import contextmanager
>
> class FileReader:
> def __init__(self, filename, isSkip):
> self.path = filename
> self.isSkip = isSkip
>
> @contextmanager
> def open(self, *args):
> from StringIO import StringIO
> lines = range(10)
> if self.isSkip:
> lines[0] = "skipped"
> lines[6] = "field1-from-line6,field2-from-line6"
> else:
> lines[0] = "field1-from-line1,field2-from-line1"
> yield StringIO("\r\n".join(map(str, lines)))
>
> def is_arbitrary_text(self,fieldnames):
> return "skipped" in fieldnames
>
> def readData(self):
> with self.open(self.path, "r") as f:
> reader = csv.DictReader(f)
> if self.is_arbitrary_text(reader.fieldnames):
> for _ in range(5):
> next(reader, None)
>
> reader._fieldnames = None # underscore necessary,
> # fieldnames setter doesn't work
> reader.fieldnames # used for its side-effect
>
> for row in reader:
> print row
>
> if __name__ == "__main__":
> import sys
> print "Demo with made-up data"
> skip = len(sys.argv) > 1 and sys.argv[1] == "--skip"
> if skip:
> print "Variant 2, header is skipped"
> else:
> print "Variant 1, no header"
> FileReader("whatever.csv", skip).readData()
>
> $ python csv_skip_header_oo.py
> Demo with made-up data
> Variant 1, no header
> {'field2-from-line1': None, 'field1-from-line1': '1'}
> {'field2-from-line1': None, 'field1-from-line1': '2'}
> {'field2-from-line1': None, 'field1-from-line1': '3'}
> {'field2-from-line1': None, 'field1-from-line1': '4'}
> {'field2-from-line1': None, 'field1-from-line1': '5'}
> {'field2-from-line1': None, 'field1-from-line1': '6'}
> {'field2-from-line1': None, 'field1-from-line1': '7'}
> {'field2-from-line1': None, 'field1-from-line1': '8'}
> {'field2-from-line1': None, 'field1-from-line1': '9'}
> $ python csv_skip_header_oo.py --skip
> Demo with made-up data
> Variant 2, header is skipped
> {'field1-from-line6': '7', 'field2-from-line6': None}
> {'field1-from-line6': '8', 'field2-from-line6': None}
> {'field1-from-line6': '9', 'field2-from-line6': None}
>
> A script using real data would be:
>
> $ cat csv_skip_header_oo.py
> import csv
>
> class FileReader:
> def __init__(self, filename):
> self.path = filename
>
> def is_arbitrary_text(self, fieldnames):
> # XXX replace with a test suitable for your actual data
> return "skipped" in fieldnames
>
> def read_data(self):
> with open(self.path, "rb") as f:
> reader = csv.DictReader(f)
> if self.is_arbitrary_text(reader.fieldnames):
> for _ in range(5):
> next(reader, None)
> reader = csv.DictReader(f)
> for row in reader:
> print row
>
> if __name__ == "__main__":
> import argparse
> parser = argparse.ArgumentParser()
> parser.add_argument("file")
> args = parser.parse_args()
>
> FileReader(args.file).read_data()
>
> $ cat skipped_header.csv
> skipped
> 1
> 2
> 3
> 4
> 5
> field1-from-line6,field2-from-line6
> 7
> 8
> 9$python csv_skip_header_oo.py skipped_header.csv
> {'field1-from-line6': '7', 'field2-from-line6': None}
> {'field1-from-line6': '8', 'field2-from-line6': None}
> {'field1-from-line6': '9', 'field2-from-line6': None}
> $ cat no_header.csv
> field1-from-line1,field2-from-line1
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9$python csv_skip_header_oo.py no_header.csv
> {'field2-from-line1': None, 'field1-from-line1': '1'}
> {'field2-from-line1': None, 'field1-from-line1': '2'}
> {'field2-from-line1': None, 'field1-from-line1': '3'}
> {'field2-from-line1': None, 'field1-from-line1': '4'}
> {'field2-from-line1': None, 'field1-from-line1': '5'}
> {'field2-from-line1': None, 'field1-from-line1': '6'}
> {'field2-from-line1': None, 'field1-from-line1': '7'}
> {'field2-from-line1': None, 'field1-from-line1': '8'}
> {'field2-from-line1': None, 'field1-from-line1': '9'}
>
> Please have a look at the cleaned-up implementation of the read_data()
> method of this last example. As a result of the discussion on the bug
> tracker <http://bugs.python.org/issue20004> I am now convinced that you
> should use two `DictReader`s rather than hack internal attributes or broken
> properties.
>
> See also <http://www.python.org/dev/peps/pep-0008/> for naming conventions.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Reading csv file Igor Korot <ikorot01@gmail.com> - 2013-12-20 00:27 -0800
csiph-web