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


Groups > comp.lang.python > #99214

Re: Problem to read from array

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From BartC <bc@freeuk.com>
Newsgroups comp.lang.python
Subject Re: Problem to read from array
Date Sat, 21 Nov 2015 15:26:34 +0000
Organization A noiseless patient Spider
Lines 58
Message-ID <n2q2bd$rf0$1@dont-email.me> (permalink)
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
Injection-Date Sat, 21 Nov 2015 15:24:29 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="cf45b3961a050227b1103bebc3cbc15a"; logging-data="28128"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/0rNR4V3C5oMy4cu3Lc9Bt"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0
In-Reply-To <2141d7ae-1064-49f3-bf36-ff663fca0ffc@googlegroups.com>
Cancel-Lock sha1:VLj7CDNFpxqPBSwrARnS3+6nkmA=
Xref csiph.com comp.lang.python:99214

Show key headers only | View raw


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.

-- 
BartC

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