A string of values, separated by commas or semicolons, in a Python list

I am reading a list of email addresses from a configuration file. Addresses can be separated by a comma or semicolon - for example,

billg@microsoft.com,steve@apple.com, dhh@37signals.com
billg@microsoft.com;steve@apple.com;  dhh@37signals.com

I also want to get rid of the blanks around email addresses.

I need to include them in a Python list as follows:

['billg@microsoft.com', 'steve@apple.com', 'dhh@37signals.com']

What is the most pythonic way to do this? Thank.

+3
source share
7 answers

In this case, I use the re module

>>> import re
>>> 
>>> data = "billg@microsoft.com;steve@apple.com;  dhh@37signals.com"
>>> stuff = re.split(r"\s*[,;]\s*", data.strip())
+10
source

Regular expressions are powerful and probably the way here; but for something simple like this, string methods are OK too. Here's a short 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']
+6
source

';' ",", , , string.split:

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

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

EDIT. :

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

You can use string.maketrans to replace multiple delimiters with spaces in one pass

import string

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

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

You can do this using only Python string controls:

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']
+1
source
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']

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

0
source


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
-1
source

Source: https://habr.com/ru/post/1789054/


All Articles