multiple delimiters

re module version

import re


string_out = """ace, bda; des; edf;fsa; gas;   ace
b
e"""



def split_re(origin_string='', separators=','):

    origin_list = re.split(r'[%s]' % separators, origin_string)

    # if you also want the delimiters:
    # origin_list = re.split(r'([%s])' % separators, origin_string)

    total_list = []
    for data in origin_list:
        if data != '' and data not in total_list:
            total_list.append(data)

    return total_list


result = ','.join(split_re(string_out, '\n,+;;,、 '))

print(result)

my version

string_out = """ace, bda; des; edf;fsa; gas;   ace
b
e"""


def split_simple(origin_string='', separators=','):
    origin_list = [origin_string]

    # get different list from different separator
    for sep in separators:

        sep_list = []
        for r in origin_list:
            for i in r.split(sep):
                sep_list.append(i.strip())

        origin_list = sep_list

    # remove none and repeat value
    total_list = []
    for index, data in enumerate(origin_list):
        if index == len(origin_list):
            break
        else:
            if data != '' and data not in total_list:
                total_list.append(data)

    return total_list


result = ','.join(split_simple(string_out, '\n,+;;,、 '))

print(result)

Python 3 version

from functools import reduce


def split_by_separator(origin_string='', separators=','):
    origin_list = [origin_string]
    for sep in separators:
        tmp_each = []
        for r in origin_list:
            tmp_each.extend(map(lambda x: x.strip(), r.split(sep)))
            print('tmp_each: ', tmp_each)
        origin_list = tmp_each
    tmp_total = []
    [tmp_total.append(data) for data in origin_list if data != '']
    return reduce(lambda x, y: y in x and x or x + [y], [[], ] + tmp_total)


string_out = ' ;vickey; hello; world; hey;how; are; \na、b,cd'

result = ','.join(split_by_separator(string_out, '\n,+;;,、'))

print(result)

Python 2 version

#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time     : 2017/3/9 19:52
# @Author   : otfsenter
# @File     : a.py

#coding:utf-8

result = '''
sdf-asd
sdf-asd01
sdf-asd02
sdf-asd,sdf-asd01 ,sdf-asd02
aui+otfsenter+which
'''

# result = ''
# with open('tmp.txt', 'r') as f:
#     for i in f:
#         result += i
#
# print result

def split_by_separator(string='', separators=','):
    rst = [string]
    for sep in separators:
        tmp = []
        for r in rst:
            tmp.extend(map(lambda x: x.strip(), r.split(sep)))
        rst = tmp
    list_tmp = []
    [list_tmp.append(data) for data in rst if data != '']
    return reduce(lambda x, y: y in x and x or x + [y], [[], ] + list_tmp)

print split_by_separator(result, '\n,+')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

idlewith

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值