How to store the contents of a file into a list?

Emile van Sebille emile at fenx.com
Sat Jan 6 15:20:10 EST 2001


As you read lines from your file, each line is returned as a
list element.  You still need to parse the individual lines
into fields:

>>> l1 = ['rec11,rec12,rec13','rec21,rec22,rec23']
>>> [x.split(',') for x in l1]
[['rec11', 'rec12', 'rec13'], ['rec21', 'rec22', 'rec23']]
>>>

HTH

--

Emile van Sebille
emile at fenx.com
-------------------


"Daniel" <Daniel.Kinnaer at AdValvas.be> wrote in message
news:3a576f75.41616667 at news.skynet.be...
>
> Hi guys. As a newbie exercise, I've been trying the
following :
>
> l1 = []
> l2 = ['rec11','rec12','rec13']
> l3 = ['rec21','rec22','rec23']
> l1.append(l2)
> l1.append(l3)
>
> l1 now is [['rec11', 'rec12', 'rec13'], ['rec21', 'rec22',
'rec23']]
>
> I've been trying to do the same when reading these records
from a file
> (The file contains 2 lines :
> rec11,rec12,rec13
> rec21,rec22,rec23)
>
> What needs to be done to obtain the same l1 as above? I've
been trying
> various ways but my l1 always reads ['rec11, rec12,
rec13', 'rec21,
> rec22, rec23'] instead of [['rec11', 'rec12', 'rec13'],
['rec21',
> 'rec22', 'rec23']].  What am I doing wrong?  All help
greatly
> appreciated.
>
> Thanks
>
> Daniel





More information about the Python-list mailing list