s = '''Are you going to Scarborough FairParsley, sage, rosemary and thymeRemember me to one who lives therehe once was atrue love of mineTell him to make me a cambric shirtParsley'''
a1 = '\n'
for i in range(len(a)):
if a[i] == ',' or a[i] == ' ':
a = a[:i] + a1 + a[i + 1:]
print(a)
如果是其他字符就不行了,应该怎么改进。
s = '''Are you going to Scarborough FairParsley, sage, rosemary and thymeRemember me to one who lives therehe once was atrue love of mineTell him to make me a cambric shirtParsley'''
a1 = '\n'
r = s.replace(' ', a1)
print(r)
如有帮助,望采纳,谢谢
先将英文标点符号替换成空格,再一并将空格替换为换行符,就可以了:
import string
r=s.replace(string.punctuation,' ').replace(' ','\n')
print(r)
s = '''Are you going to Scarborough FairParsley,
sage, rosemary and thymeRemember me to one who
lives therehe once was atrue love of mineTell
him to make me a cambric shirtParsley'''
import re
a = re.sub(r'\,|\.|\n','', s).replace(' ', '\n')
print(a)
首先将‘,.\n’转化为空格,然后再将所有的空格转化为换行符
用正则表达式啊
import re
s = '''Are you I'm going to Scarborough FairParsley,sage,rosemary and thymeRemember me to one who lives therehe once was atrue love of mineTell him to make me a cambric shirtParsley'''
a1 = '\n'
s = re.sub(r"[^A-Za-z\']+",a1,s)
print(s)
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632