Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > read file into list of lists

Reply
Thread Tools

read file into list of lists

 
 
antar2
Guest
Posts: n/a
 
      07-11-2008
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
 
Reply With Quote
 
 
 
 
Laurent Rahuel
Guest
Posts: n/a
 
      07-11-2008
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


 
Reply With Quote
 
 
 
 
bockman@virgilio.it
Guest
Posts: n/a
 
      07-11-2008
On 11 Lug, 15:15, antar2 <(E-Mail Removed)> 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
 
Reply With Quote
 
Paddy
Guest
Posts: n/a
 
      07-11-2008
On Jul 11, 2:15 pm, antar2 <(E-Mail Removed)> 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.
 
Reply With Quote
 
Gerard flanagan
Guest
Posts: n/a
 
      07-11-2008
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[i]

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.

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      07-11-2008
On Jul 11, 11:35 pm, Paddy <(E-Mail Removed)> wrote:
> On Jul 11, 2:15 pm, antar2 <(E-Mail Removed)> 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']

 
Reply With Quote
 
Paddy
Guest
Posts: n/a
 
      07-12-2008
On Jul 11, 9:32*pm, John Machin <(E-Mail Removed)> wrote:
> On Jul 11, 11:35 pm, Paddy <(E-Mail Removed)> wrote:
>
>
>
> > On Jul 11, 2:15 pm, antar2 <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Breaking Python list into set-length list of lists Jason Python 7 02-12-2009 04:12 PM
list of lists of lists .... yomgui Python 6 07-31-2006 07:28 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file. Tony Perl Misc 5 04-19-2004 03:28 PM
Howto: extract a 'column' from a list of lists into a new list? Greg Brunet Python 7 07-02-2003 03:20 AM



Advertisments