Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99215

Re: Problem to read from array

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 <nhilterbrand@gmail.com>
Newsgroups comp.lang.python
Subject Re: Problem to read from array
Date Sat, 21 Nov 2015 10:52:06 -0500
Lines 71
Message-ID <mailman.40.1448121134.2291.python-list@python.org> (permalink)
References <2141d7ae-1064-49f3-bf36-ff663fca0ffc@googlegroups.com> <n2q2bd$rf0$1@dont-email.me>
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 <nhilterbrand@gmail.com>
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 <n2q2bd$rf0$1@dont-email.me>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99215

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Problem to read from array vostrushka@gmail.com - 2015-11-21 02:41 -0800
  Re: Problem to read from array Peter Otten <__peter__@web.de> - 2015-11-21 12:23 +0100
  Re: Problem to read from array Laura Creighton <lac@openend.se> - 2015-11-21 15:22 +0100
  Re: Problem to read from array Laura Creighton <lac@openend.se> - 2015-11-21 16:05 +0100
  Re: Problem to read from array BartC <bc@freeuk.com> - 2015-11-21 15:26 +0000
    Re: Problem to read from array Nathan Hilterbrand <nhilterbrand@gmail.com> - 2015-11-21 10:52 -0500
      Re: Problem to read from array Crane Ugly <vostrushka@gmail.com> - 2015-11-23 12:58 -0800
        Re: Problem to read from array sohcahtoa82@gmail.com - 2015-11-23 16:44 -0800
          Re: Problem to read from array Crane Ugly <vostrushka@gmail.com> - 2015-11-24 00:56 -0800
            Re: Problem to read from array Peter Otten <__peter__@web.de> - 2015-11-24 10:31 +0100

csiph-web