read file into list of lists

Paddy paddy3118 at googlemail.com
Sat Jul 12 07:49:30 CEST 2008


On Jul 11, 9:32 pm, John Machin <sjmac... at lexicon.net> wrote:
> On Jul 11, 11:35 pm, Paddy <paddy3... at googlemail.com> wrote:
>
>
>
> > On Jul 11, 2:15 pm, antar2 <desoth... at yahoo.com> wrote:
>
> > > Hello,
>
> > > I can not find out how to read a file into a list of lists. I know how
> > > to split a text into a list
>
> > > sentences = line.split(\n)
>
> > > following text for example should be considered as a list of lists (3
> > > columns and 3 rows), so that when I make the print statement list[0]
> > > [0], that the word pear appears
>
> > > pear noun singular
> > > books nouns plural
> > > table noun singular
>
> > > Can someone help me?
>
> > > Thanks
>
> > lofl = [line.strip().split() for line in the_opened_file]
>
> >>> line = '   foo   bar   '
> >>> line.strip().split()
> ['foo', 'bar']
> >>> line.split()
>
> ['foo', 'bar']

Thanks , ta.

- Paddy.



More information about the Python-list mailing list