python - Python中的单引号换行
问题描述
我现在在 Python 中有这个:
"Going to school.
Taking a few courses and playing basketball.
Got a new puppy."
"Going to school.
I bought a new backpack yesterday.
Got a new cat.
I did my homework as well."
"Going to school.
Brought lunch today."
我试图弄清楚我是如何从任何时候开始在这里放置换行符的,"
所以我在每行的引号中都有句子。
我认为正则表达式可能是一种方式,但不确定。有什么建议吗?
解决方案
只需使用re.DOTALL
flag 提取引号内的数据以像任何其他字符一样考虑 endline,并使用“非贪婪”模式
t = """"Going to school.
Taking a few courses and playing basketball.
Got a new puppy."
"Going to school.
I bought a new backpack yesterday.
Got a new cat.
I did my homework as well."
"Going to school.
Brought lunch today." """
import re
print(re.findall('".*?"',t,flags=re.DOTALL))
在引号内打印提取的句子列表。
['"Going to school.\nTaking a few courses and playing basketball.\nGot a new puppy."',
'"Going to school.\nI bought a new backpack yesterday.\nGot a new cat.\nI did my homework as well."',
'"Going to school.\nBrought lunch today."']
现在我们正确地提取了数据,用换行符加入字符串列表并用空格替换内部换行符现在很容易:
print("\n".join([x.replace("\n"," ") for x in re.findall('".*?"',t,flags=re.DOTALL)]))
输出:
"Going to school. Taking a few courses and playing basketball. Got a new puppy."
"Going to school. I bought a new backpack yesterday. Got a new cat. I did my homework as well."
"Going to school. Brought lunch today."
推荐阅读
- javascript - 如何使用javascript更改数据集(div)中的文本(CLASS DOESN'T WORK)
- android - 从从firebase检索的图像视图中设置壁纸 - android
- logic - 创建一个对给定 x 值的多项式进行微分的函数?
- google-apps-script - 从自己的驱动器访问文件时如何使用更窄的 Google Apps 脚本授权范围
- assembly - 如何编码子立即 83 / 5 操作码的操作数,如 sub edx、0x3a?
- django - 如何将 Django 访问者文件上传到新的子目录,目录名称应该是会话名称?
- reactjs - 我们如何在扩展面板中跟踪每个表单的状态
- reactjs - Bing Maps V8 控件呈现空 div
- assembly - 为什么这不会改变内存中 array1 中某些元素的值?
- r - 将数据连接到 .kmz 或 .kml 并绘图