String of values separated by commas or semicolons into a Python list
I'm reading a list of email addresses from a config file. The addresses can be delimited by comma or semicolon - e.g.,
billg@microsoft.com,steve@apple.com, dhh@37signals.com
billg@microsoft.com;steve@apple.com; dhh@37signals.com
开发者_运维知识库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:
['billg@microsoft.com', 'steve@apple.com', 'dhh@37signals.com']
What's the most Pythonic way to do it? Thanks.
In this case I whould use the re module
>>> import re
>>>
>>> data = "billg@microsoft.com;steve@apple.com; dhh@37signals.com"
>>> stuff = re.split(r"\s*[,;]\s*", data.strip())
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 = "billg@microsoft.com,steve@apple.com, dhh@37signals.com"
>>> s2 = " billg@microsoft.com;steve@apple.com; dhh@37signals.com "
>>> print [s.strip() for s in s1.replace(',', ';').split(';')]
['billg@microsoft.com', 'steve@apple.com', 'dhh@37signals.com']
>>> print [s.strip() for s in s2.replace(',', ';').split(';')]
['billg@microsoft.com', 'steve@apple.com', 'dhh@37signals.com']
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']
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']
You could do it using just Python's string manipulation facilities:
import string
s1 = "billg@microsoft.com,steve@apple.com, dhh@37signals.com"
s2 = "billg@microsoft.com;steve@apple.com; dhh@37signals.com"
print s1.translate(string.maketrans(';',','), string.whitespace).split(',')
# ['billg@microsoft.com', 'steve@apple.com', 'dhh@37signals.com']
print s2.translate(string.maketrans(';',','), string.whitespace).split(',')
# ['billg@microsoft.com', 'steve@apple.com', 'dhh@37signals.com']
data = ''' billg@microsoft.com,steve@apple.com, dhh@37signals.com
billg@microsoft.com;steve@apple.com;\t \rdhh@37signals.com '''
print repr(data),'\n'
import re
print re.findall('[^,\s;]+', data)
result
' billg@microsoft.com,steve@apple.com, dhh@37signals.com \n billg@microsoft.com;steve@apple.com;\t \rdhh@37signals.com '
['billg@microsoft.com', 'steve@apple.com', 'dhh@37signals.com', 'billg@microsoft.com', 'steve@apple.com', 'dhh@37signals.com']
notice the '\n' , '\t' and '\r' in this data
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
精彩评论