read file into list of lists

A

antar2

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
 
L

Laurent Rahuel

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
===============================================================
 
B

bockman

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:
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
 
P

Paddy

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.
 
G

Gerard flanagan

antar2 said:
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

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.
 
J

John Machin

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?

lofl = [line.strip().split() for line in the_opened_file]
line = ' foo bar '
line.strip().split() ['foo', 'bar']
line.split()
['foo', 'bar']
 
P

Paddy

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top