找到你要的答案

Q:Python: split string with delimiters from a list

Q:Python:从列表分隔符分开的字符串

I'd like to split a string with delimiters which are in a list.

The string has this pattern: Firstname, Lastname Email

The list of delimiters has this: [', ',' '] taken out of the pattern.

I'd like to split the string to get a list like this ['Firstname', 'Lastname', 'Email']

For a better understanding of my problem, this is what I'm trying to achieve:

The user shall be able to provide a source pattern: %Fn%, %Ln% %Mail% of data to be imported and a target pattern how the data shall be displayed:

%Ln%%Fn%; %Ln%, %Fn; %Mail%

This is my attempt:

data = "Firstname, Lastname Email"

for delimiter in source_pattern_delimiter:
    prog = re.compile(delimiter)
    data_tuple = prog.split(data)

How do I 'merge' the data_tuple list(s)?

我想把与分隔符的列表中的字符串。

字符串有这种模式:FirstName、LastName的电子邮件

分隔符列表中有:[ ',',' ]采取的模式。

I'd like to split the string to get a list like this ['Firstname', 'Lastname', 'Email']

为了更好地理解我的问题,这正是我所要达到的:

The user shall be able to provide a source pattern: %Fn%, %Ln% %Mail% of data to be imported and a target pattern how the data shall be displayed:

%Ln%%Fn%; %Ln%, %Fn; %Mail%

这是我的尝试:

data = "Firstname, Lastname Email"

for delimiter in source_pattern_delimiter:
    prog = re.compile(delimiter)
    data_tuple = prog.split(data)

我如何“合并”data_tuple列表(S)?

answer1: 回答1:
import re

re.split(re.compile("|".join([", ", " "])), "Firstname, Lastname Email")

hope it helps

import re

re.split(re.compile("|".join([", ", " "])), "Firstname, Lastname Email")

希望有帮助

answer2: 回答2:

Seems you want something like this,

>> s = "Firstname, Lastname Email"
>>> delim = [', ',' ']
>>> re.split(r'(?:' + '|'.join(delim) + r')', s)
['Firstname', 'Lastname', 'Email']

似乎你想要这样的东西,

>> s = "Firstname, Lastname Email"
>>> delim = [', ',' ']
>>> re.split(r'(?:' + '|'.join(delim) + r')', s)
['Firstname', 'Lastname', 'Email']
answer3: 回答3:

A solution without regexes and if you want to apply a particular delimiter at a particular position:

def split(s, delimiters):
    for d in delimiters:
        item, s = s.split(d, 1)
        yield item
    else:
        yield s

>>> list(split("Firstname, Lastname Email", [", ", " "]))
["Firstname", "Lastname", "Email"]

一个无解的正则表达式,如果你想在某个特定的位置,运用特定的分隔符:

def split(s, delimiters):
    for d in delimiters:
        item, s = s.split(d, 1)
        yield item
    else:
        yield s

>>> list(split("Firstname, Lastname Email", [", ", " "]))
["Firstname", "Lastname", "Email"]
answer4: 回答4:

What about splitting on spaces, then removing any trailing commas?

>>> data = "Firstname, Lastname Email"
>>> [s.rstrip(',') for s in data.split(' ')]
['Firstname', 'Lastname', 'Email']

关于分裂的空间,然后删除任何尾随逗号吗?

>>> data = "Firstname, Lastname Email"
>>> [s.rstrip(',') for s in data.split(' ')]
['Firstname', 'Lastname', 'Email']
answer5: 回答5:

You are asking for a template based way to reconstruct the split data. The following script could give you an idea how to progress. It first splits the data into the three parts and assigns each to a dictionary entry. This can then be used to give a target pattern:

import re

data = "Firstname, Lastname Email"

# Find a list of entries and display them
entries = re.findall("(\w+)", data)
print entries       

# Convert the entries into a dictionary
dEntries = {"Fn": entries[0], "Ln": entries[1], "Mail": entries[2]}

# Use dictionary-based string formatting to provide a template system
print "%(Ln)s%(Fn)s; %(Ln)s, %(Fn)s; %(Mail)s" % dEntries

This displays the following:

['Firstname', 'Lastname', 'Email']
LastnameFirstname; Lastname, Firstname; Email

If you really need to use the exact template system you have provided then the following could be done to first convert your target pattern into one suitable for use with Python's dictionary system:

def display_with_template(data, target_pattern):
    entries = re.findall("(\w+)", data)
    dEntries = {"Fn": entries[0], "Ln": entries[1], "Mail": entries[2]}

    for item in ["Fn", "Ln", "Mail"]:
        target_pattern= target_pattern.replace("%%%s%%" % item, "%%(%s)s" % item)

    return target_pattern % dEntries

print display_with_template("Firstname, Lastname Email", r"%Ln%%Fn%; %Ln%, %Fn%; %Mail%")

Which would display the same result, but uses a custom target pattern:

LastnameFirstname; Lastname, Firstname; Email

您正在请求基于模板的方法来重建分割数据。下面的脚本可以告诉你如何进步。它首先将数据分成三个部分,并将每个分配给字典条目。然后可以使用它来给出目标模式:

import re

data = "Firstname, Lastname Email"

# Find a list of entries and display them
entries = re.findall("(\w+)", data)
print entries       

# Convert the entries into a dictionary
dEntries = {"Fn": entries[0], "Ln": entries[1], "Mail": entries[2]}

# Use dictionary-based string formatting to provide a template system
print "%(Ln)s%(Fn)s; %(Ln)s, %(Fn)s; %(Mail)s" % dEntries

显示如下:

['Firstname', 'Lastname', 'Email']
LastnameFirstname; Lastname, Firstname; Email

如果你真的需要使用精确的模板系统提供了之后你可以做第一个转换你的目标模式为一个适合使用Python的字典系统:

def display_with_template(data, target_pattern):
    entries = re.findall("(\w+)", data)
    dEntries = {"Fn": entries[0], "Ln": entries[1], "Mail": entries[2]}

    for item in ["Fn", "Ln", "Mail"]:
        target_pattern= target_pattern.replace("%%%s%%" % item, "%%(%s)s" % item)

    return target_pattern % dEntries

print display_with_template("Firstname, Lastname Email", r"%Ln%%Fn%; %Ln%, %Fn%; %Mail%")

将显示相同的结果,但使用自定义目标模式:

LastnameFirstname; Lastname, Firstname; Email
python  regex  string  split