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.
--
http://mail.python.org/mailman/listinfo/python-list