2 Replies - 710 Views - Last Post: 11 July 2013 - 05:57 PM Rate Topic: -----

#1 ZeroTorrent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 68
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 Nallo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 162
  • View blog
  • Posts: 248
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1