如何使用不同分隔符切分字符串,把某个字符串依据分隔符拆分,该字符包含不同的多种分隔符,如下

s = '12;;7.osjd;.jshdjdknx+' 其中 ; . + 是分隔符

有哪些解决方案?

方法1:通过str.split()方法,每次处理一个分隔符

def go_split(s, symbol):result = [s]for i in symbol:median = []# 普通方法# for x in result:#     median.extend(x.split(i)# 列表解析# [median.extend(y.split(i)) for y in result if y]# map高阶函数,map生成可迭代对象for z in map(lambda x: x.split(i), result):median.extend(z)      # 以上三个方法都可以解决问题result = median   # 去除空字符串return [x for x in result if x]
if __name__ == "__main__":# 定义初始字符串s = '12;;7.osjd;.jshdjdknx+'# 定义分隔符symbol = ';./+'result = go_split(s, symbol)print(result)

方法2:通过re.split()方法,一次性拆分所有字符串,推荐

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import redef go_split(s, symbol):# 拼接正则表达式symbol = "[" + symbol + "]+"# 一次性分割字符串result = re.split(symbol, s)# 去除空字符return [x for x in result if x]if __name__ == "__main__":# 定义初始字符串s = '12;;7.osjd;.jshdjdknx+'# 定义分隔符symbol = ';./+'result = go_split(s, symbol)print(result)

一个文本转语音的小程序,对文本进行分割后,一句话一句话的读出来:

import pyttsx3
import re
import os
engine = pyttsx3.init()
# 分割原文
def go_split(s, symbol):# 拼接正则表达式symbol = "[" + symbol + "]+"# 一次性分割字符串result = re.split(symbol, s)# 去除空字符return [x for x in result if x]
# 判断字符串只包含中文:
def reflag(list):for i in range(len(list)):for ch in list[i]:if ch >= '\u4e00' and ch <= '\u9fa5':return i
def reader(list,flag):if flag == None:print("本文没有中文翻译")else:print("本文有中文翻译")lenght = len(list)i = 0while i <lenght:instr = input("输入操作:"+"\n""1:继续听读本句话"+"\n""2:继续听读下一句话"+"\n"+":")if instr=="1":engine.say(list[i])print(list[i])engine.runAndWait()if instr == "2":i = i + 1print(list[i])engine.say(list[i])engine.runAndWait()print(list)
def start():dirlsit= os.listdir('reader')for i in range(len(dirlsit)):print(str(i)+":"+str(dirlsit[i]))choice = int(input("输入选择的文章序号:"))with open("reader/"+dirlsit[choice],'r',encoding='utf-8') as f:article = f.read()article = article.replace('\n', '')# 定义分隔符symbol = ',;.:'list = go_split(article,symbol)if filsh(list):flag = reflag(list) - 1else:flag=Nonerate = engine.getProperty('rate')engine.setProperty('rate', rate-25)reader(list,flag)start()

文章格式:

Night after night, she came to tuck me in, even long after my childhood years. Following her longstanding custom, she'd lean down and push my long hair out of the way, then kiss my forehead.
I don't remember when it first started annoying me — her hands pushing my hair that way. But it did annoy me, for they felt work-worn and rough against my young skin. Finally, one night, I shouted out at her, "Don't do that anymore —your hands are too rough!" She didn't say anything in reply. But never again did my mother close out my day with that familiar expression of her love.
Time after time, with the passing years, my thoughts returned to that night. By then I missed my mother's hands, missed her goodnight kiss on my forehead. Sometimes the incident seemed very close, sometimes far away. But always it lurked, in the back of my mind.
Well, the years have passed, and I'm not a little girl anymore. Mom is in her mid-seventies, and those hands I once thought to be so rough are still doing things for me and my family. She's been our doctor, reaching into a medicine cabinet for the remedy to calm a young girl's stomach or soothe the boy's scraped knee. She cooks the best fried chicken in the world... gets stains out of blue jeans like I never could...
Now, my own children are grown and gone. Mom no longer has Dad, and on special occasions, I find myself drawn next door to spend the night with her. So it was late on Thanksgiving Eve, as I slept in the bedroom of my youth, a familiar hand hesitantly run across my face to brush the hair from my forehead. Then a kiss, ever so gently, touched my brow.
In my memory, for the thousandth time, I recalled the night my young voice complained, "Don't do that anymore — your hands are too rough!" Catching Mom's hand in hand, I blurted out how sorry I was for that night. I thought she'd remember, as I did. But Mom didn't know what I was talking about. She had forgotten — and forgiven — long ago.
That night, I fell asleep with a new appreciation for my gentle mother and her caring hands. And the guilt that I had carried around for so long was nowhere to be found.
【中文译文】:
母亲总是在我入睡之后,为我掖好被子,然后俯下身子,轻轻拨开覆在我脸上的长发,亲吻我的前额。日复一日,母亲一直保持着这个习惯,即使我已不再是小孩子了,这一切却依然故我。
不知从什么时候开始,母亲的这种习惯渐渐让我感到不悦----我不喜欢她那双布满老茧的手就这样划过我细嫩的皮肤。终于,在一个夜晚,我忍不住冲她吼了起来:“你不要再这样了,你的手好粗糙!”母亲无言以对。但从此却再没有用这种我熟悉的表达爱的方式来为我的一天画上句号。
日子一天天过去,随着时间的流逝,我却总是不由得想起那一夜。我开始想念母亲的那双手,想念她印在我前额上的“晚安”。这种渴望忽远忽近,但始终潜藏在我心灵深处的某个角落。
若干年后,我成熟了,已不再是个小女孩了。母亲也已到了古稀之年,可她却始终没有停止过操劳,用她那双曾经被我视为“粗糙”的手为我和我的家庭做着力所能及的事情。她是我们的家庭医生,小姑娘胃痛时,她会从药箱里找出胃药来,小男孩擦伤的膝盖时,她会去安抚他的伤痛。她能做出世界上最好吃的炸鸡,能把蓝色牛仔裤上的污渍去得毫无痕迹......
现在,我自己的孩子也已长大,有了自己的生活,母亲却没有了父亲的陪伴。有一次,恰好是感恩节前夜,我决定就睡在母亲旁边的卧室里,陪她度过这一夜。这是我儿时的卧室,一切都是那么的熟悉,还有一只熟悉的手犹豫着从我的脸上掠过,梳理着我前额的头发,然后,一个吻,带着一如往日的温柔,轻轻落在了我的额头。
在我的记忆里,曾几千次再现那晚的情景和我那稚嫩的抱怨声:“你不要再这样了,你的手好粗糙!”我一把抓住母亲的手,一股脑说出我对那一晚深深的愧疚。我想,她一定和我一样,对那晚的事历历在目。然而,母亲却不知我再说些什么-----她早忘了,早已原谅我了。
那天晚上,我带着对母亲新的感激安然入睡,我感激她的温柔,和她那呵护的双手。多年来压在我心头的负罪感也随之烟消云散。

Python如何使用不同分隔符切分字符串相关推荐

  1. python分隔符怎么用_如何使用不同分隔符切分字符串

    如何使用不同分隔符切分字符串,把某个字符串依据分隔符拆分,该字符包含不同的多种分隔符,如下 s = '12;;7.osjd;.jshdjdknx+' 其中 ; . + 是分隔符 有哪些解决方案? 方法 ...

  2. Python按照指定的分隔符拆分字符串split()函数

    [小白从小学Python.C.Java] [Python全国计算机等级考试] [Python数据分析考试必会题] ● 标题与摘要 Python按照指定的分隔符拆分字符串 split()函数 ● 选择题 ...

  3. Python使用多个分隔符分割字符串

    本文收录于<Python开发>专栏,此专栏聚焦于Python开发中的编程技巧和总结,将持续更新,欢迎大家订阅! 个人主页:有梦想的程序星空 个人介绍:小编是人工智能领域硕士,全栈工程师,深 ...

  4. python怎么拆分没有分隔符字符串_python如何拆分含有多种分隔符的字符串

    案例: 把某个字符串依据分隔符拆分,该字符包含不同的多种分隔符,如下 s = '12;;7.osjd;.jshdjdknx+' 其中 ; . + 是分隔符 有哪些解决方案? 方法1:通过str.spl ...

  5. 【Python教程】两种方法教你拆分含有多种分隔符的字符串

    方法一:连续使用str.spilt()方法,每次处理一种分隔符: str.split()方法的应用:按某一种分隔符分割字符串: #不能用单词字符进行分割(字母.数字.下划线) A.str.split( ...

  6. python 分隔符截取字符串_python指定分隔符截取字符串

    python截取指定分隔符后面的字符串 例如:notifydata[1] =G20-杭州峰会放假时间 notifydata[2] =G20-杭州峰print('-'.join('G20-杭州峰会-放假 ...

  7. python键盘输入字符串以空格为分隔符_已知str1=“ You need Python” , 以空格为分隔符,分割此此字符串,然后用“ @” 为连接符,将其连接起来()。_学小易找答案...

    [单选题]增加同步总线带宽的手段有很多,但以下( )不能提高总线带宽 [填空题]已知str1=" You need Python" , 获得字符串的字符总数(). [单选题]-40 ...

  8. 基于多种分隔符进行字符串的分割

    首先来说,字符串尾部的不叫分隔符,字符串中间的才叫分隔符. 例如 Hello World, My name is Python. 中的点号就不是分隔符 将下面这个字符串切分成单个的单词. Hello ...

  9. python入门2——基础语法2——字符串详解

    目录 前言 字符串 拼接字符串 字符串索引 字符串切片 字符串不可变性 字符串大小转换方法 字符串分割.组合与移除 字符串定位与替换方法 总结 前言 字符串是 Python 中最常用的数据类型.我们可 ...

最新文章

  1. 独家 | 王海峰:百度大数据与人工智能(附PPT下载)
  2. 类的初始化(构造函数)
  3. 定义一个计算字符串有效长度的_一个正方形的小抽屉柜,根据设计草图计算出所需四片木板的长度...
  4. 后台开发人员面试内容——数据库(二)
  5. Caffe学习系列(17):模型各层数据和参数可视化
  6. python集合类型的操作符_Python 语言学习 第一篇:数据类型(数字,集合,布尔类型,操作符)...
  7. 二元运算 FFT+分治
  8. mysql5.718免安装教程_mysql5.7.18版本免安装配置教程
  9. MySQL 常用命令大全
  10. 经典算法题每日演练——第十八题 外排序
  11. 搭建直播平台,Android端直播APP源码如何实现礼物效果
  12. Java 设计模式 --- Builder模式 Java Builder 模式
  13. 【C#实现猜数字游戏】
  14. HDU 4313 最小生成树
  15. 小松的STM32教程(2)—— OLED显示
  16. 开机自动随机改计算机名,【优化电脑·禁止随机启动项·加速开机】
  17. 荣之学:跨境电商和淘宝哪个好?
  18. A14:Untiy+Leapmotion制作拍打乐符游戏
  19. oneshot一次性服务
  20. 用matlab做谱分析程序,经典功率谱分析Matlab程序

热门文章

  1. 《中国人工智能学会通讯》——1.27 智能助手的核心价值
  2. 《易学Python》——1.4 Linux
  3. 快速排序算法C#实现
  4. 各类双绞线标准、用法、接法速记
  5. ASP.NET AJAX入门系列(9):在母版页中使用UpdatePanel
  6. 增强 用文本增强修改SAP标准屏幕中的字段名称 属于元素的文本增强
  7. 把剪贴板里面的数据放到内表
  8. 王峰记忆法 解密王峰记忆法之迷
  9. SAP MM模块相关透明表收集
  10. 5分钟,带你领略项目经理十年的功力