Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50243 > unrolled thread
| Started by | alex.hanga@gmail.com |
|---|---|
| First post | 2013-07-09 06:30 -0700 |
| Last post | 2013-07-09 08:07 -0700 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
Reading File Into 2D List alex.hanga@gmail.com - 2013-07-09 06:30 -0700
Re: Reading File Into 2D List Dave Angel <davea@davea.name> - 2013-07-09 10:24 -0400
Re: Reading File Into 2D List alex.hanga@gmail.com - 2013-07-09 08:07 -0700
| From | alex.hanga@gmail.com |
|---|---|
| Date | 2013-07-09 06:30 -0700 |
| Subject | Reading File Into 2D List |
| Message-ID | <3b5b02ee-90a3-4a19-af96-b83b4a1039a2@googlegroups.com> |
Hello!
I'm new here and fairly new to Python. I am attempting to read a data file into python and adding it to a 2D list to make it easy to use further down the line.
My data file is just 7 numbers in a row seperated by commas and each bulk of data is seperated by the sign @ to indicate that the data from this point on should be stored into a new part of the 2D list.
1,1,1,1,1,1,1
2,2,2,2,2,2,2
@
3,3,3,3,3,3,3
4,4,4,4,4,4,4
After reading the file, the way I imagine the data to be shown would be in this manner:
data[0][0] = (1,1,1,1,1,1,1)
data[0][1] = (2,2,2,2,2,2,2)
data[1][0] = (3,3,3,3,3,3,3)
data[1][1] = (4,4,4,4,4,4,4)
This way it will be easy to loop across the data when I use it in Blender.
My code looks like this;
------------------------------
object_data = []
object_data.append([])
for rows in data.splitlines():
elems = rows.split(',')
if elems[0] != "@":
object_data[i].append((float(elems[0]),float(elems[1]),
float(elems[2]),float(elems[3]),float(elems[4]),
float(elems[5]),int(elems[6])))
else:
**start on object_data[1][j] and loop over it all again**
-------------------------------
I could really use some help as to how I would force my code to not start on object_data[0] on the next iteration in the for loop, but rather on object_data[1]. I'm an avid Matlab user so I imagined this to be done simply by setting i=i+1 in the else part, however this does not work as it complains about the list being out of bounds.
Any help is really appreciated!
[toc] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-07-09 10:24 -0400 |
| Message-ID | <mailman.4452.1373379861.3114.python-list@python.org> |
| In reply to | #50243 |
On 07/09/2013 09:30 AM, alex.hanga@gmail.com wrote:
> Hello!
>
> I'm new here and fairly new to Python. I am attempting to read a data file into python and adding it to a 2D list to make it easy to use further down the line.
>
> My data file is just 7 numbers in a row seperated by commas and each bulk of data is seperated by the sign @ to indicate that the data from this point on should be stored into a new part of the 2D list.
>
> 1,1,1,1,1,1,1
> 2,2,2,2,2,2,2
> @
> 3,3,3,3,3,3,3
> 4,4,4,4,4,4,4
>
Perhaps you mean:
data = """\
1,1,1,1,1,1,1
2,2,2,2,2,2,2
@
3,3,3,3,3,3,3
4,4,4,4,4,4,4
"""
> After reading the file, the way I imagine the data to be shown would be in this manner:
>
> data[0][0] = (1,1,1,1,1,1,1)
> data[0][1] = (2,2,2,2,2,2,2)
> data[1][0] = (3,3,3,3,3,3,3)
> data[1][1] = (4,4,4,4,4,4,4)
perhaps you mean:
object_data[0][0] == (1,1,1,1,1,1)
etc.
>
> This way it will be easy to loop across the data when I use it in Blender.
>
> My code looks like this;
>
> ------------------------------
> object_data = []
> object_data.append([])
You omitted i = 0
>
> for rows in data.splitlines():
So exactly what is data? It's not what you say above. Did you get it
by doing something like myfile.read() ?
> elems = rows.split(',')
> if elems[0] != "@":
> object_data[i].append((float(elems[0]),float(elems[1]),
> float(elems[2]),float(elems[3]),float(elems[4]),
> float(elems[5]),int(elems[6])))
> else:
>
> **start on object_data[1][j] and loop over it all again**
> -------------------------------
>
> I could really use some help as to how I would force my code to not start on object_data[0] on the next iteration in the for loop, but rather on object_data[1]. I'm an avid Matlab user so I imagined this to be done simply by setting i=i+1 in the else part, however this does not work as it complains about the list being out of bounds.
>
> Any help is really appreciated!
>
Replace te **start line with something like:
object_data.append([])
i += 1
This assumes a few missing lines, which must have been there or you
would have already had runtime errors. For example, you'll need i=0
before the loop.
Another comment about the append with all those calls to float(). When
you see a line like that, you want to seriously consider making it a
loop, or a comprehension, or something.
Given that elem is a list of strings, you could convert it to a list of
float, then convert that to a tuple (if you really wanted that). At
that point, you just append it. All this could be done in one line if
you like, something like (untested):
object_data[i].append(tuple(map(float, row.split(","))))
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | alex.hanga@gmail.com |
|---|---|
| Date | 2013-07-09 08:07 -0700 |
| Message-ID | <bb4592bf-dabf-409b-927f-21cd095e3452@googlegroups.com> |
| In reply to | #50249 |
> Replace te **start line with something like:
>
>
>
> object_data.append([])
>
> i += 1
>
>
>
> This assumes a few missing lines, which must have been there or you
>
> would have already had runtime errors. For example, you'll need i=0
>
> before the loop.
>
>
>
> Another comment about the append with all those calls to float(). When
>
> you see a line like that, you want to seriously consider making it a
>
> loop, or a comprehension, or something.
>
>
>
> Given that elem is a list of strings, you could convert it to a list of
>
> float, then convert that to a tuple (if you really wanted that). At
>
> that point, you just append it. All this could be done in one line if
>
> you like, something like (untested):
>
>
>
> object_data[i].append(tuple(map(float, row.split(","))))
>
>
>
>
>
>
>
> --
>
> DaveA
Yes, indeed there are a few lines missing. Just the read file and the i=0 pretty much. The help you gave me solved my problem, thank you very much!
I know the calls to float() are sub par and quite stupidly made but I'm not that good at python scripting (yet) so I try to keep my functions rather easy to read so I understand what is happening. A bit later I will take the time to make my code more sophisticated, but as of now I just want it to work, hehe.
Again, thank you so much!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web