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


Groups > comp.lang.python > #44304

Re: Scan CSV file and saving it into an array

Path csiph.com!usenet.pasdenom.info!aioe.org!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 <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'that?': 0.05; 'assignment': 0.07; 'column': 0.07; 'detect': 0.07; 'subject:file': 0.07; 'exception,': 0.09; 'imply': 0.09; 'rows': 0.09; 'subject:into': 0.09; 'python': 0.11; 'creates': 0.14; 'useful,': 0.14; 'array.': 0.16; 'build?': 0.16; 'clause.': 0.16; 'confuse': 0.16; 'csv': 0.16; 'numpy': 0.16; 'parameter.': 0.16; 'subject:CSV': 0.16; 'subject:Scan': 0.16; 'subject:array': 0.16; 'traceback.': 0.16; 'for?': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'result.': 0.19; 'things.': 0.19; 'code,': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'affects': 0.24; 'copied': 0.24; 'integer': 0.24; 'precise': 0.24; 'header': 0.24; 'script': 0.25; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; "doesn't": 0.30; 'lines': 0.31; 'use?': 0.31; 'file': 0.32; 'probably': 0.32; 'url:python': 0.33; 'reader': 0.33; 'sense': 0.34; 'could': 0.34; 'problem': 0.35; 'case,': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'representing': 0.36; 'next': 0.36; 'url:org': 0.36; 'error.': 0.37; 'wrong': 0.37; 'two': 0.37; 'list': 0.37; 'expected': 0.38; 'saves': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'does': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'break': 0.61; 'full': 0.61; 'first': 0.61; 'here:': 0.62; 'save': 0.62; 'such': 0.63; 'field': 0.63; 'skip:n 10': 0.64; 'more': 0.64; '(that': 0.65; 'received:74.208': 0.68; 'statement,': 0.68; 'behavior': 0.77; 'ana': 0.84; "it'd": 0.84; 'presumably': 0.84; 'received:74.208.4.194': 0.84; 'textbook': 0.84; 'either:': 0.91; 'obvious,': 0.91
Date Wed, 24 Apr 2013 20:01:13 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5
MIME-Version 1.0
To python-list@python.org
Subject Re: Scan CSV file and saving it into an array
References <1533da3c-a297-41a0-a575-968a86120a5b@googlegroups.com>
In-Reply-To <1533da3c-a297-41a0-a575-968a86120a5b@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 8bit
X-Provags-ID V02:K0:LBgh338B5lDazHnp9aR5z/pVrOlBV9TbL07Gbj0Dcyf KClQpN8cDbvQmP86P5WSpVA8bvcmk8YPSwqw9jB98Cex7FVGgM u39g2BRQ0zQfTOkXuCxS8IxChmzP1Jfe8Fy83pMoL27Q9TRuaA 62Ua0su8M1gvmGpdwNkHrJ8orzFCbrjZMed3BbyogQ/7l75jek puwMjuriRuBd6AH00UBSbmqVPj4jhtFrAWumkf7gZ6B/uK9J0e 5XSNgym4WsumXBDfHelXKWuH57iGXWQOrUvSIfaI9aApmP2B1q Pfl/DRdH0VbMcnVdEH/WEHXubYFYuE0uHiOCn8z1U4Whifr5Q= =
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1040.1366848092.3114.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366848092 news.xs4all.nl 15992 [2001:888:2000:d::a6]:38992
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:44304

Show key headers only | View raw


On 04/24/2013 07:01 PM, Ana Dionísio wrote:
> Hello!
>
> I have this script that scans a csv file and if the value in the first column == 200 it saves that row into an array.

No it doesn't. It creates a list, then overwrites it with a numpy array, 
then overwrites that with a list of strings representing one row.

If you want to really use a Python array, then read here:

    http://docs.python.org/2/library/array.html

It'd probably be best to start with a precise problem statement, 
presumably copied from your textbook or assignment sheet.  What python 
version is this for?  And what OS?  (that affects whether you need a 
file mode)  Exactly what data structure are you trying to build?  What 
type of a csv file are you trying to use?  Is there a standard header 
line?  How big might the file be?  What behavior do you want if there's 
no line that begins with the field "200"?  Or if there's more than one 
such line?  Or if there are less than 10 lines following it in the file? 
  What about a field of "0200"?

>
> The problem is, I need to save that row and the next 10 rows in that same array. What can I add to the script so it does that? I tried to do for row in len(10): but I get an error.
>

When you say "get an error" it could be one of many things.  In this 
case, it's obvious, since len() doesn't make sense with an integer 
parameter.  But in general you want to say either:

1) it gave me the wrong result.  I expected AAAA and got BBBB
2) it did nothing at all.
3) it gave an exception, and here's the full traceback.


>
> p = csv.reader(open('file.csv'), delimiter=';')
> a=[0]*2881
> a = numpy.array(a, dtype=dict)

These two lines do nothing useful, and they confuse the reader of the 
code, since they imply that the list will end up of size 2881, and/or as 
a numpy array.

> for row in p:
>         if row[0]=="200":
>            a=row
>            break

missing else clause. How do you detect that there was no match?

> print a
>


-- 
DaveA

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


Thread

Scan CSV file and saving it into an array Ana Dionísio <anadionisio257@gmail.com> - 2013-04-24 16:01 -0700
  Re: Scan CSV file and saving it into an array "Rhodri James" <rhodri@wildebst.demon.co.uk> - 2013-04-25 00:21 +0100
    Re: Scan CSV file and saving it into an array Dave Angel <davea@davea.name> - 2013-04-24 19:47 -0400
  Re: Scan CSV file and saving it into an array Dave Angel <davea@davea.name> - 2013-04-24 20:01 -0400
  Re: Scan CSV file and saving it into an array Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-25 01:21 +0100

csiph-web