Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44298 > unrolled thread
| Started by | Ana Dionísio <anadionisio257@gmail.com> |
|---|---|
| First post | 2013-04-24 16:01 -0700 |
| Last post | 2013-04-25 01:21 +0100 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Ana Dionísio <anadionisio257@gmail.com> |
|---|---|
| Date | 2013-04-24 16:01 -0700 |
| Subject | Scan CSV file and saving it into an array |
| Message-ID | <1533da3c-a297-41a0-a575-968a86120a5b@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | "Rhodri James" <rhodri@wildebst.demon.co.uk> |
|---|---|
| Date | 2013-04-25 00:21 +0100 |
| Message-ID | <op.wv2i5sz7a8ncjz@gnudebeest> |
| In reply to | #44298 |
On Thu, 25 Apr 2013 00:01:01 +0100, Ana Dionísio <anadionisio257@gmail.com> wrote: > I tried to do for row in len(10): but I get an error. Please remember to tell us what error, with the traceback, so that we can make fun of you for not reading it :-) In this case it's pretty obvious; Python will complain that you can't call len() on an integer (because it's not a container type, so the whole idea of "length" is meaningless). You probably meant to use "range(10)" instead of "len(10)", but then "row" would be the integers from 0 to 9 inclusive, which isn't what you want either. What you actually want to do is "row = p.next()" (in Python 2.x) or "row = next(p)" (in Python 3.x) to get the next row from the CSV file, add it to the array and repeat until you have ten more lines. Another for-loop will make this less tedious to write. Odd that this subject should have come up so many times in various guises in the last week or two. -- Rhodri James *-* Wildebeest Herder to the Masses
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-04-24 19:47 -0400 |
| Message-ID | <mailman.1038.1366847254.3114.python-list@python.org> |
| In reply to | #44299 |
On 04/24/2013 07:21 PM, Rhodri James wrote: > On Thu, 25 Apr 2013 00:01:01 +0100, Ana Dionísio > <anadionisio257@gmail.com> wrote: > <SNIP> > > Odd that this subject should have come up so many times in various > guises in the last week or two. > Not that odd. In a larger classroom I'd expect several of the students to have enough internet savvy to find a forum like this. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-04-24 20:01 -0400 |
| Message-ID | <mailman.1040.1366848092.3114.python-list@python.org> |
| In reply to | #44298 |
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
[toc] | [prev] | [next] | [standalone]
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2013-04-25 01:21 +0100 |
| Message-ID | <mailman.1042.1366849332.3114.python-list@python.org> |
| In reply to | #44298 |
On 25 April 2013 00:01, Ana Dionísio <anadionisio257@gmail.com> 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 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)
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web