我现在在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."
我试图弄清楚如果"
发生时我在这里放置换行符,所以我的句子在每一行都有引号。
我认为正则表达式可能是方式,但不确定。有什么建议吗?
答案 0 :(得分:1)
使用re.DOTALL
标志提取引号内的数据,以像其他任何字符一样考虑结束,并使用"非贪婪"模式
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."