3

I'm reading a list of email addresses from a config file. The addresses can be delimited by comma or semicolon - e.g.,

[email protected],[email protected], [email protected]
[email protected];[email protected];  [email protected]

I'd like to get rid of any whitespace around the email addresses too.

I need to get them into a Python list like this:

['[email protected]', '[email protected]', '[email protected]']

What's the most Pythonic way to do it? Thanks.

7 Answers 7

10

In this case I whould use the re module

>>> import re
>>> 
>>> data = "[email protected];[email protected];  [email protected]"
>>> stuff = re.split(r"\s*[,;]\s*", data.strip())
Sign up to request clarification or add additional context in comments.

2 Comments

And use data.strip() to get rid of leading whitespace before the first and trailing whitespace after the last.
woops, forgot that. .strip() added. Thanks!
6

Regular expressions are powerful, and probably the way to go here; but for something as simple as this, string methods are OK too. Here's a terse solution:

[s.strip() for s in s1.replace(',', ';').split(';')]

Test output:

>>> s1 = "[email protected],[email protected], [email protected]"
>>> s2 = "  [email protected];[email protected];  [email protected]  "
>>> print [s.strip() for s in s1.replace(',', ';').split(';')]
['[email protected]', '[email protected]', '[email protected]']
>>> print [s.strip() for s in s2.replace(',', ';').split(';')]
['[email protected]', '[email protected]', '[email protected]']

Comments

1

If it's only ';' or only ',' and you know which, use string.split:

>>> 'adjifjdasf;jdiafjodafs;jdiajof'.split(';')
['adjifjdasf', 'jdiafjodafs', 'jdiajof']

http://docs.python.org/library/stdtypes.html#str.split

EDIT For whitespace you can also do:

>>> map(str.strip, 'adjifjdasf;jdiafjodafs ; jdiajof'.split(';'))
['adjifjdasf', 'jdiafjodafs', 'jdiajof']

3 Comments

There's also the issue of whitespace.
you may have forgotten that Ambrosio wants to get rid of leading and trailing whitespaces...
Doesn't handle both cases (i.e. can be ',' or ';' delimited).
1

You can use string.maketrans to replace multiple separators with spaces in a single pass

import string

data = "one  two,  three ; four "
stuff = [i for i in data.translate(string.maketrans(";,", "  ")).split()]

print stuff   # -> ['one', 'two', 'three', 'four']

Comments

1

You could do it using just Python's string manipulation facilities:

import string

s1 = "[email protected],[email protected], [email protected]"
s2 = "[email protected];[email protected];  [email protected]"

print s1.translate(string.maketrans(';',','), string.whitespace).split(',')
# ['[email protected]', '[email protected]', '[email protected]']
print s2.translate(string.maketrans(';',','), string.whitespace).split(',')
# ['[email protected]', '[email protected]', '[email protected]']

Comments

0
data = '''   [email protected],[email protected], [email protected]  
  [email protected];[email protected];\t  \[email protected]       '''

print repr(data),'\n'

import re

print re.findall('[^,\s;]+', data)

result

'   [email protected],[email protected], [email protected]  \n  [email protected];[email protected];\t  \[email protected]       ' 

['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']

notice the '\n' , '\t' and '\r' in this data

Comments

-1


def gen_list(file_path):
    read= open(file_path, "r")
    split1= read.split(";")
    new_list= []
    for i in split1:
       split2 = i.split(",")
       split_list = [item.strip() for item in split2 if "@" in item]
       new_list.extend(split_list)
       return new_list

# This works for both comma and ;. The number of lines can further be reduced

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.