博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
multiple delimiters
阅读量:6220 次
发布时间:2019-06-21

本文共 2669 字,大约阅读时间需要 8 分钟。

re module version

import restring_out = """ace, bda; des; edf;fsa; gas;   acebe"""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_listresult = ','.join(split_re(string_out, '\n,+;;,、 '))print(result)

my version

string_out = """ace, bda; des; edf;fsa; gas;   acebe"""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_listresult = ','.join(split_simple(string_out, '\n,+;;,、 '))print(result)

Python 3 version

from functools import reducedef 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-8result = '''sdf-asdsdf-asd01sdf-asd02sdf-asd,sdf-asd01 ,sdf-asd02aui+otfsenter+which'''# result = ''# with open('tmp.txt', 'r') as f:#     for i in f:#         result += i## print resultdef 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,+')

转载于:https://www.cnblogs.com/otfsenter/p/6544416.html

你可能感兴趣的文章
mono for android software自动更新
查看>>
版本管理工具——Git和TortoiseGit(乌龟Git)
查看>>
开源SIP服务器加密软件NethidPro升级
查看>>
linux的简单命令
查看>>
我的友情链接
查看>>
大型网站技术架构(一)大型网站架构演化
查看>>
百度页面分享插件源代码
查看>>
易宝典文章——玩转Office 365中的Exchange Online服务 之六 了解Exchange Online对于邮箱使用的限制...
查看>>
确定jdk是32位版本还是64位版本
查看>>
linux下文件删除的原理
查看>>
python os.path模块
查看>>
评"抄袭就是生产力:评中国盛行的成功学逻辑"
查看>>
红客专用电脑安全工具箱 v 1.9
查看>>
$.noConflict()方法--常用的方式
查看>>
MongoDB的真正性能
查看>>
使用Zabbix监控windows服务
查看>>
win7 从硬盘重装操作系统(64位)
查看>>
CentOS-7安装Kubernetes-1.12.1
查看>>
如何进行感恩节的营销策划工作
查看>>
CCNP学习之路之GLBP
查看>>