Reputation: 31
I'm trying to read line into a list where each word on that line is different argument. For example when my text file contains:
Word1, Word2, Some different words,separated by comma,but no space
Word3, Word4, Some different words,separated by comma,but no space
I would like to get lists like that:
['Word1', 'Word2', 'Some different words,separated by comma,but no space'],
['Word3', 'Word4', 'Some different words,separated by comma,but no space']
Maybe I can even get list like this:
['Word1', 'Word2', 'Some different words','separated by comma', 'but no space']
So far I've managed to get this work when there is one line in text file by reading each word into list.
list_words = f.read().split()
It gives me output:
['Word1', 'Word2', 'Some different words,separated by comma,but no space']
How could I do this when I have multiple lines? Also if I later want to print out first arguments from both lists, can I use list_words[0] and it will give me automatically 'Word1' and 'Word3' ?
I hope this explanation was clear enough.
Upvotes: 1
Views: 3750
Reputation: 117856
You could use the following list comprehension
list_words = [i.split(',') for i in f]
Upvotes: 3
Reputation: 16556
If you want to split with a comma followed by a space, you could use re.split
:
>>> with open('f.txt') as f:
... print [re.split(', ',line) for line in f]
...
[['Word1', 'Word2', 'Some different words,separated by comma,but no space\n'],
['Word3', 'Word4', 'Some different words,separated by comma,but no space\n']]
If you want to split on every comma, just use str.split:
>>> with open('f.txt') as f:
... print [line.split(',') for line in f]
...
[['Word1', ' Word2', ' Some different words', 'separated by comma', 'but no space\n'],
['Word3', ' Word4', ' Some different words', 'separated by comma', 'but no space\n']]
you can use strip
to get rid of the \n
:
>>> with open('f.txt') as f:
... print [line.strip().split(',') for line in f]
... # or print [re.split(', ',line.strip()) for line in f]
...
[['Word1', ' Word2', ' Some different words', 'separated by comma', 'but no space'],
['Word3', ' Word4', ' Some different words', 'separated by comma', 'but no space']]
In fact, you can also use line.strip().split(', ')
. I just forgot that you can have a delimiter of more than 1 character…
Upvotes: 2