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


Groups > comp.lang.python > #62176

Re: Reading csv file

Path csiph.com!usenet.pasdenom.info!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'else:': 0.03; 'column': 0.07; 'none,': 0.07; 'subject:file': 0.07; 'sys': 0.07; 'assuming': 0.09; 'complicate': 0.09; 'filename': 0.09; 'mysql.': 0.09; 'none)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'report,': 0.09; 'rows': 0.09; 'underscore': 0.09; 'python': 0.11; 'def': 0.12; 'missed': 0.12; 'creates': 0.14; '(without': 0.16; 'at,': 0.16; 'csv': 0.16; 'guys,': 0.16; 'igor': 0.16; 'open()': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'skipping': 0.16; 'stringio': 0.16; 'subject:Reading': 0.16; 'switches': 0.16; 'task.': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'not,': 0.20; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'parse': 0.24; 'replace': 0.24; 'skip': 0.24; 'skip:{ 20': 0.24; 'text,': 0.24; 'header': 0.24; 'file.': 0.24; 'push': 0.26; 'skip:" 30': 0.26; 'switch': 0.26; 'header:X-Complaints-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'dec': 0.30; 'skip:@ 10': 0.30; 'lines': 0.31; 'posting': 0.31; 'info.': 0.31; 'names.': 0.31; 'file': 0.32; 'run': 0.32; 'guess': 0.33; 'reader': 0.33; 'actual': 0.34; '(2)': 0.35; 'but': 0.35; 'located': 0.36; 'i.e.': 0.36; 'yield': 0.36; 'hi,': 0.36; 'thank': 0.38; 'to:addr:python- list': 0.38; 'does': 0.39; 'embedded': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'read': 0.60; 'first': 0.61; 'complete': 0.62; 'such': 0.63; 'reply': 0.66; 'realized': 0.68; 'deeply': 0.69; "'1'}": 0.84; "'2'}": 0.84; 'otten': 0.84; 'sorry.': 0.91; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Reading csv file
Date Tue, 17 Dec 2013 11:51:04 +0100
Organization None
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>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b322.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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.4275.1387277432.18130.python-list@python.org> (permalink)
Lines 99
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1387277432 news.xs4all.nl 2935 [2001:888:2000:d::a6]:51167
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:62176

Show key headers only | View raw


Igor Korot wrote:

> Hi, guys,
> 
> On Tue, Dec 17, 2013 at 12:55 AM, Peter Otten <__peter__@web.de> wrote:
>> Peter Otten wrote:
>>
>>> You are still reading the complete csv file. Assuming
>>>
>>> (1) the first row of the csv contains the column names
>>> (2) you want to skip the first five rows of data
> 
> Looking at the Peter's reply I realized I missed very important piece:
> 
> The first row may or may not contain column names.
> If it does not, the first row will just contain some text, i.e. "abc"
> and the column names will be located on the row 6.
> 
> I know if does complicate things but I am deeply sorry.
> The csv file is generated by some program run and I guess depending on
> the switches passed to
> that program it either creates the header in the csv (report name,
> time slice it ran at, user it ran under
> and some other info.
> Or it can be run without such switch and then it generates a normal csv.
> 
> The report it generates is huge: it has about 30+ fields and I need to
> read this report, parse it and
> push accordingly to the database of mySQL.
> 
> Thank you for any suggestions and sorry for not posting complete task.

Try the following (without the mock-ups of course):


$ cat csv_skip_header.py
import csv
import sys
from contextlib import contextmanager

filename = "ignored"

@contextmanager
def open(*args):
    "mock-up, replace with open() built-in"
    from StringIO import StringIO
    lines = range(10)
    if len(sys.argv) > 1 and sys.argv[1] == "--skip":
        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(fieldnames):
    "mock-up, replace with the actual check"
    return "skipped" in fieldnames

with open(filename, "rb") as f:
    reader = csv.DictReader(f)
    if 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
$ python csv_skip_header.py 
{'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.py --skip
{'field1-from-line6': '7', 'field2-from-line6': None}
{'field1-from-line6': '8', 'field2-from-line6': None}
{'field1-from-line6': '9', 'field2-from-line6': None}

You may find the following a bit cleaner:

with open(filename, "rb") as f:
    reader = csv.reader(f)
    fieldnames = next(reader)
    if is_arbitrary_text(fieldnames):
        for _ in range(5):
            next(reader, None)
        fieldnames = None
    reader = csv.DictReader(f, fieldnames=fieldnames)
    for row in reader:
        print row

Or you do the skipping on the file (only if the rows don't have embedded 
newlines).

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


Thread

Re: Reading csv file Peter Otten <__peter__@web.de> - 2013-12-17 11:51 +0100

csiph-web