python - 如何使用 Python 格式化字符串
问题描述
我得到了一个字符串,如下所示
s = 'This is sentence 1."This is sentence 2." This is sentence 3.'
我想要如下的输出
This is sentence 1.
"This is sentence 2."
This is sentence 3.
我为此编写了以下代码
s = 'This is sentence 1."This is sentence 2." This is sentence 3.'
for i in s.replace('.','.\n').split('\n'):
print(i.strip())
下面是我得到的输出
This is sentence 1.
"This is sentence 2.
" This is sentence 3.
问题在于双引号的句子。
如果我可以编写一个可以区分 . 和。”然后我可以解决我的问题。
解决方案
我会尽力给你一个提示。尝试添加一个IF 语句,让你的代码决定你想要做什么。如果您想将代码与更大的字符串一起使用。
推荐阅读
- asp.net-web-api - 以编程方式将 [AllowAnonymous] 属性添加到我的所有控制器方法
- jenkins-plugins - Jenkins - 通过 CIFS 插件错误发布
- javascript - REACT & Expressjs:后端不能“服务”静态前端
- java - 将参数化类型实例引用到其原始类型和使用原始类型引用另一个原始类型实例之间的区别?
- java - 循环错误时如何解决此问题?
- python-3.x - 无法按类名或 xpath selenium python 找到元素
- javascript - 烧瓶传递 json 和 render_template
- web-applications - 如果安装了使用 facebook 应用程序的动态链接
- mysql - 如何在 MySQL 中拆分文本和数字?
- java - Android Studio - 帮助我理解这段代码