read file into list of lists

Jeffrey Froman jeffrey at fro.man
Fri Jul 11 11:23:24 EDT 2008


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




More information about the Python-list mailing list