Split a list to individual entries
I am extracting some emails from a CSV file and then saving it to another CSV file.
email variable should be in this format:
email = ['email@email.com'], ['email2@company.com'], ['e开发者_StackOverflow社区mail3@company2.com']
but in certain cases it will be returned as:
email = ['email@email.com', 'email2@email.com'], ['email3@email.com']
In certain rows it finds 2 emails, so that is when it is presented like this.
What would be an efficient way to change it??
The next should be quite efficient:
>>> import itertools
>>> data = [ ['email@email.com', 'email2@email.com'], ['email3@email.com'] ]
>>> [[i] for i in itertools.chain(*data)]
[['email@email.com'], ['email2@email.com'], ['email3@email.com']]
data = [ ['email@email.com', 'email2@email.com'], ['email3@email.com'] ]
def flatten(data):
for item in data:
if isinstance(item, basestring):
yield item
else:
for i in item:
yield [i]
or, if you want to support arbitrary levels of nesting:
def flatten(data):
for item in data:
if isinstance(item, basestring):
yield [item]
else:
for i in flatten(item):
yield i
If you only need a list of emails, without each element wrapped in a list (which seems more reasonable to me), the solution is much simpler:
import itertools
print list(itertools.chain.from_iterable(data))
If you are working with CSV files you may want to try the CSV module from the standard library. http://docs.python.org/library/csv.html
Example:
$ cat > test.csv
['email@email.com', 'email2@email.com'], ['email3@email.com']
$ python
>>> import csv
>>> reader = csv.reader(open('test.csv', 'r'))
>>> for row in reader:
... print row
...
["['email@email.com'", " 'email2@email.com']", " ['email3@email.com']"]
What I did there may not be what you want but if you look at the library you might find what you need.
精彩评论