简体   繁体   中英

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 - eg,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM