read file into list of lists

Discussion in 'Python' started by antar2, Jul 11, 2008.

  1. antar2

    antar2 Guest

    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
    antar2, Jul 11, 2008
    #1
    1. Advertising

  2. Hello,

    A way to do it

    ===============================================================
    from __future__ import with_statement

    res = []
    with open("sentences.txt","r") as f:
    sentences = [elem for elem in f.read().split('\n') if elem]
    for sentence in sentences:
    res.append(sentence.split())

    print res
    ===============================================================

    antar2 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
    Laurent Rahuel, Jul 11, 2008
    #2
    1. Advertising

  3. antar2

    Guest

    On 11 Lug, 15:15, antar2 <> 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



    You can use split again, using ' ' or nothing(defaults to whitespace
    characters) as separator,
    like this:

    >>> text = """pear noun singular

    books nouns plural
    table noun singular"""

    >>> words = [ x.split() for x in text.split('\n') ]
    >>> print words

    [['pear', 'noun', 'singular', ''], ['books', 'nouns', 'plural', ''],
    ['table', 'noun', 'singular']]


    Ciao
    -----
    FB
    , Jul 11, 2008
    #3
  4. antar2

    Paddy Guest

    On Jul 11, 2:15 pm, antar2 <> 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]

    - Paddy.
    Paddy, Jul 11, 2008
    #4
  5. antar2 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?
    >



    class Table(object):

    def __init__(self, text=None):
    self.rows = []
    if text:
    self.write(text)

    def write(self, text):
    self.rows.extend(line.split() for line in text.splitlines())

    def read(self):
    return '\n'.join(' '.join(row) for row in self.rows)

    def __getitem__(self, i):
    return self.rows

    def __iter__(self):
    return iter(self.rows)

    table = Table()

    table.write('apple orange coconut')

    print table[0][1]

    print table.read()

    table.write('clematis rose lily')

    print table[1][2]

    print table.read()


    for row in table:
    print row



    (If you have quoted items, it is more difficult)

    G.
    Gerard flanagan, Jul 11, 2008
    #5
  6. antar2

    John Machin Guest

    On Jul 11, 11:35 pm, Paddy <> wrote:
    > On Jul 11, 2:15 pm, antar2 <> 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']
    John Machin, Jul 11, 2008
    #6
  7. antar2

    Paddy Guest

    On Jul 11, 9:32 pm, John Machin <> wrote:
    > On Jul 11, 11:35 pm, Paddy <> wrote:
    >
    >
    >
    > > On Jul 11, 2:15 pm, antar2 <> 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.
    Paddy, Jul 12, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Brunet
    Replies:
    7
    Views:
    423
    Greg Brunet
    Jul 2, 2003
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    403
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. yomgui

    list of lists of lists ....

    yomgui, Jul 28, 2006, in forum: Python
    Replies:
    6
    Views:
    311
    yomgui
    Jul 31, 2006
  4. bahoo
    Replies:
    3
    Views:
    297
    Bruno Desthuilliers
    Apr 3, 2007
  5. Jason
    Replies:
    7
    Views:
    4,459
Loading...

Share This Page