read file into list of lists

Laurent Rahuel lrahuel.notgood at voila.fr
Fri Jul 11 15:28:09 CEST 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
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: sentences.txt
URL: <http://mail.python.org/pipermail/python-list/attachments/20080711/3053222a/attachment.txt>


More information about the Python-list mailing list