read file into list of lists

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • antar2

    read file into list of lists

    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

    #2
    Re: read file into list of lists

    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(sent ence.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

    Comment

    • bockman@virgilio.it

      #3
      Re: read file into list of lists

      On 11 Lug, 15:15, antar2 <desoth...@yaho o.comwrote:
      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(default s 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

      Comment

      • Paddy

        #4
        Re: read file into list of lists

        On Jul 11, 2:15 pm, antar2 <desoth...@yaho o.comwrote:
        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().sp lit() for line in the_opened_file]

        - Paddy.

        Comment

        • Gerard flanagan

          #5
          Re: read file into list of lists

          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.exten d(line.split() for line in text.splitlines ())

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

          def __getitem__(sel f, i):
          return self.rows[i]

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

          table = Table()

          table.write('ap ple orange coconut')

          print table[0][1]

          print table.read()

          table.write('cl ematis 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.

          Comment

          • Jeffrey Froman

            #6
            Re: read file into list of lists

            Laurent Rahuel wrote that antar2 wrote:
            >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
            File objects are themselves iterable, returning one line per iteration. So a
            simple approach is:
            >>table = [line.split() for line in open('sentences .txt')]
            >>table[0][0]
            'pear'


            Jeffrey

            Comment

            • John Machin

              #7
              Re: read file into list of lists

              On Jul 11, 11:35 pm, Paddy <paddy3...@goog lemail.comwrote :
              On Jul 11, 2:15 pm, antar2 <desoth...@yaho o.comwrote:
              >
              >
              >
              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().sp lit() for line in the_opened_file]
              >
              >>line = ' foo bar '
              >>line.strip(). split()
              ['foo', 'bar']
              >>line.split( )
              ['foo', 'bar']

              Comment

              • Paddy

                #8
                Re: read file into list of lists

                On Jul 11, 9:32 pm, John Machin <sjmac...@lexic on.netwrote:
                On Jul 11, 11:35 pm, Paddy <paddy3...@goog lemail.comwrote :
                >
                >
                >
                On Jul 11, 2:15 pm, antar2 <desoth...@yaho o.comwrote:
                >
                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().sp lit() for line in the_opened_file]
                >
                >line = '   foo   bar   '
                >line.strip().s plit()
                ['foo', 'bar']
                >line.split()
                >
                ['foo', 'bar']
                Thanks , ta.

                - Paddy.

                Comment

                Working...