# Reading file into 2D List

Page 1 of 1

## 2 Replies - 2744 Views - Last Post: 11 July 2013 - 05:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=324512&amp;s=56b7ecd72be8df1d78566398395735f4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ZeroTorrent

Reputation: 0
• Posts: 1
• Joined: 09-July 13

# Reading file into 2D List

Posted 09 July 2013 - 06:13 AM

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;

```i = 0
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][0] 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!

Is This A Good Question/Topic? 0

## Replies To: Reading file into 2D List

### #2 woooee

Reputation: 27
• Posts: 109
• Joined: 21-November 12

## Re: Reading file into 2D List

Posted 09 July 2013 - 09:08 AM

Quote

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
Do you mean the above or the below as they are different.

Quote

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)
To do the second quote you would just read the file line by line. To do the first quote would be something like
```simulated_file="""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
5,5,5,5,5,5,5"""

simulated_data = simulated_file.split("\n")

list_of_lists = []
group_list = []
for rec in simulated_data:
rec=rec.strip()
if "@" == rec and len(group_list):  ## append this group of recs
list_of_lists.append(group_list)
group_list = []
else:
group_list.append(rec)
## final group
if len(group_list):
list_of_lists.append(group_list)

for sub_list in list_of_lists:
print sub_list
```

This post has been edited by woooee: 09 July 2013 - 09:11 AM

### #3 Nallo

• D.I.C Regular

Reputation: 165
• Posts: 258
• Joined: 19-July 09

## Re: Reading file into 2D List

Posted 11 July 2013 - 05:57 PM

ZeroTorrent, you were almost there. The only thing left is to do the right thing in the else block of you programm:
Increase the counter and add a new element for the next batch of data:
```i = 0
#using global variables is ugly

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:
i += 1 #increase the counter
object_data.append([]) #add a new (empty element) for the next data

```

Though you may want to make the code a little more robust, readable and maintainable (some hints in the comments):
```def convert_line(line):
#making this an own function is good practice
#you may change the data format later
#and this way there is one place to change in that event
elems = line.split(',')
return (float(elems[0]),float(elems[1]),float(elems[2]),float(elems[3]),
float(elems[4]),float(elems[5]),int(elems[6]))

def process_data_file(df):
DATA_SEPARATOR = "@" #magical symbols deserve an own name!!!
processed = [[]]
for line in df.splitlines():
if DATA_SEPARATOR in line:
#a little more robust, think about a ",@" typo in the data file
#compared to a line[0] == "@"
processed.append([])
else:
processed[-1].append(convert_line(line))
#using [-1] to access the last element, so no need to keep a counter variable :-)
return processed

```

This post has been edited by Nallo: 11 July 2013 - 05:58 PM