Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'else:': 0.03; '"""': 0.07; 'lines,': 0.07; 'append': 0.09; 'part,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'runtime': 0.09; 'skip:o 50': 0.09; 'that).': 0.09; 'python': 0.11; 'stored': 0.12; '"""\\': 0.16; 'appreciated!': 0.16; 'bounds.': 0.16; 'elem': 0.16; 'iteration': 0.16; 'loop.': 0.16; 'matlab': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:File': 0.16; 'subject:Reading': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'skip:f 30': 0.19; 'header:User-Agent:1': 0.23; 'errors.': 0.24; 'replace': 0.24; 'fairly': 0.24; 'looks': 0.24; 'shown': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; "i'm": 0.30; 'code': 0.31; 'assumes': 0.31; 'omitted': 0.31; 'file': 0.32; 'skip:- 30': 0.32; 'another': 0.32; 'comment': 0.34; 'could': 0.34; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'like,': 0.36; 'skip:f 40': 0.36; 'subject:List': 0.36; 'done': 0.36; 'doing': 0.36; 'next': 0.36; 'should': 0.36; 'example,': 0.37; 'skip:4 10': 0.37; 'list': 0.37; 'list.': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'that,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'read': 0.60; 'easy': 0.60; 'skip:o 30': 0.61; 'new': 0.61; 'numbers': 0.61; 'simply': 0.61; 'further': 0.61; "you'll": 0.62; 'making': 0.63; 'email addr:gmail.com': 0.63; 'here': 0.66; 'bulk': 0.74; 'float,': 0.84; 'this;': 0.91; 'imagine': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: Reading File Into 2D List Date: Tue, 09 Jul 2013 10:24:06 -0400 References: <3b5b02ee-90a3-4a19-af96-b83b4a1039a2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 174.32.174.33 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 In-Reply-To: <3b5b02ee-90a3-4a19-af96-b83b4a1039a2@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 94 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373379861 news.xs4all.nl 15883 [2001:888:2000:d::a6]:55206 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50249 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