Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Scan CSV file and saving it into an array

Reply
Thread Tools

Scan CSV file and saving it into an array

 
 
Ana Dionísio
Guest
Posts: n/a
 
      04-24-2013
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.

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.


p = csv.reader(open('file.csv'), delimiter=';')
a=[0]*2881
a = numpy.array(a, dtype=dict)
for row in p:
if row[0]=="200":
a=row
break
print a
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      04-25-2013
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
 
Reply With Quote
 
 
 
 
Oscar Benjamin
Guest
Posts: n/a
 
      04-25-2013
On 25 April 2013 00:01, Ana Dionísio <(E-Mail Removed)> 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.
>
> The problem is, I need to save that row and the next 10 rows in that samearray. What can I add to the script so it does that? I tried to do for rowin len(10): but I get an error.
>
>
> p = csv.reader(open('file.csv'), delimiter=';')
> a=[0]*2881
> a = numpy.array(a, dtype=dict)


You shouldn't be using a numpy array for this; use a list instead. I
suggest that you have a go at the Python tutorial and avoid using
numpy until you are more confident with the basics of Python itself.

The tutorial (for Python 2) is here:
http://docs.python.org/2/tutorial/


Oscar
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a CSV file into an array of struct problem (again!) GLW C++ 0 12-14-2010 08:41 AM
read and write csv file using csv module jliu66 Python 0 10-19-2007 03:12 PM
How to move data from a CSV file to a JTable, and from a JTable to a CSV file ? Tintin92 Java 1 02-14-2007 06:51 PM
Read CSV file into an array oyekomova Python 2 01-10-2007 08:09 PM
read file with while and then scan lines into array Martin Foster Perl Misc 4 12-08-2003 05:11 AM



Advertisments