Path: csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Nathan Hilterbrand Newsgroups: comp.lang.python Subject: Re: Problem to read from array Date: Sat, 21 Nov 2015 10:52:06 -0500 Lines: 71 Message-ID: References: <2141d7ae-1064-49f3-bf36-ff663fca0ffc@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 7Rmw+l35uaCxMkqZ+DXmmQp1cAZpUgKG7dD4vt95ZNCw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'line:': 0.07; 'rows': 0.09; 'index': 0.13; '10:26': 0.16; 'characters:': 0.16; 'index.': 0.16; 'param': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'subject:Problem': 0.16; 'subject:array': 0.16; 'wrote:': 0.16; 'input': 0.18; 'load': 0.20; '(not': 0.20; 'friend.': 0.22; 'parameter': 0.22; 'rid': 0.22; 'am,': 0.23; 'bit': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'header :User-Agent:1': 0.26; 'parameters': 0.27; 'skip:( 20': 0.28; 'array': 0.29; "i'm": 0.30; 'print': 0.30; 'code': 0.30; 'skip:[ 10': 0.31; 'probably': 0.31; 'message-id:@gmail.com': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'but': 0.36; 'needed': 0.36; 'lines': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'or,': 0.38; 'hi,': 0.38; 'subject:from': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'email addr:gmail.com': 0.62; 'further': 0.62; 'per': 0.62; 'charset:windows-1252': 0.62; 'more': 0.63; 'nathan': 0.84; 'subject:read': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=bjNt7kfQnTf8r6halE5a+7uKfdqbF/DxA5mun3tknXw=; b=wJ2yvGJvzm3dgnYn/eMvKz5zOglhKvqnzYQd+xgrSjLrZyW5x+OG7/svF0ZlV8ckQn bebZs3x1AFPHN/qUqSqC6OCFA6twMrM1P9jJs5tfHxs625N0xSrD9F+xx+f1e+YhnEmO nINIl2qEswUQ97bhGchJQ5HiD97BCV9q1ngbmQBvQSYMbaLcMrEorNHg4JtSMHlKTqS5 YeKrcbD0e/B/Lteiypl+XUqJxsBcP/TbRujohyUPcROUOBA5ukiatw2KD2LIp8f+HOr1 OWbG4yEHO2fn8VtQSOkg5I6KakkG/mjK9Rk2MAasG/vgR33d6icK4PWKko1WA9jhfJFs XG4w== X-Received: by 10.13.247.68 with SMTP id h65mr18196806ywf.83.1448121131571; Sat, 21 Nov 2015 07:52:11 -0800 (PST) User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99215 On 11/21/2015 10:26 AM, BartC wrote: > On 21/11/2015 10:41, vostrushka@gmail.com wrote: >> Hi, >> I have a file with one parameter per line: >> a1 >> b1 >> c1 >> a2 >> b2 >> c2 >> a3 >> b3 >> c3 >> ... >> The parameters are lines of characters (not numbers) >> >> I need to load it to 2D array for further manipulations. >> So far I managed to upload this file into 1D array: >> >> ParametersRaw = [] >> with open(file1) as fh: >> ParametersRaw = fh.readlines() >> fh.close() > > I tried this code based on yours: > > with open("input") as fh: > lines=fh.readlines() > > rows = len(lines)//3 > > params=[] > index=0 > > for row in range(rows): > params.append([lines[index],lines[index+1],lines[index+2]]) > index += 3 > > for row in range(rows): > print (row,":",params[row]) > > For the exact input you gave, it produced this output: > > 0 : ['a1\n', 'b1\n', 'c1\n'] > 1 : ['a2\n', 'b2\n', 'c2\n'] > 2 : ['a3\n', 'b3\n', 'c3\n'] > > Probably you'd want to get rid of those \n characters. (I don't know > how off-hand as I'm not often write in Python.) > > The last bit could also be written: > > for param in params: > print (params) > > but I needed the row index. > To get rid of the '\n' (lineend) characters: with open(file1) as fh: ParametersRaw = [line.strip() for line in fh.readlines()] or, more succinctly.. with open(file1) as fh: ParametersRaw = [line.strip() for line in fh] Comprehensions are your friend. Nathan