我知道我有一些类似的问题,但是我找不到解决问题的方法。 我有一个字符串是
"subject: Exercise Feedback Form
persona_id: bresse
Q1: Yes
Q1 comments: Yes everything was found A1
Q2: No
Q2 comments: No forgot to email me A2
Q3: Yes
Q3 comments: All was good A3
Q4: No
Q4 comments: It was terrible A4
Q5_comments: Get Alex to make it better
subject: Issue With App
persona_id: bresse
comments: Facebook does not work comments feedback"
如您所见,中间有大量空白。我如何使用python删除它?
答案 0 :(得分:1)
text
是您的字符串:
import re
text = re.sub(r"\s{2,}", "", text)
答案 1 :(得分:0)
您可以使用正则表达式并配置该表达式以将n个或多个空格/换行符/制表符/空格替换为一个空格:
import re
s = "hello \n world"
print(re.sub("\s{4,}"," ",s))
打印:
hello world
如果至少有四个空格,它将删除所有空格/换行符/制表符/所有内容(正则表达式中的\s
),并且将仅替换一个空格(以避免分隔的单词被整理)替换后,您可以将其替换为换行符或不包含任何字符。
答案 2 :(得分:0)
尝试一下:
s = """subject: Exercise Feedback Form
persona_id: bresse
Q1: Yes
Q1 comments: Yes everything was found A1
Q2: No
Q2 comments: No forgot to email me A2
Q3: Yes
Q3 comments: All was good A3
Q4: No
Q4 comments: It was terrible A4
Q5_comments: Get Alex to make it better
subject: Issue With App
persona_id: bresse
comments: Facebook does not work comments feedback"""
s = s.replace("\n\n","")
print(s)
答案 3 :(得分:0)
您可以使用re.sub
:
import re
print(re.sub('(?<=\n)\s+\n', '', content))
输出:
"subject: Exercise Feedback Form
persona_id: bresse
Q1: Yes
Q1 comments: Yes everything was found A1
Q2: No
Q2 comments: No forgot to email me A2
Q3: Yes
Q3 comments: All was good A3
Q4: No
Q4 comments: It was terrible A4
Q5_comments: Get Alex to make it better
subject: Issue With App
persona_id: bresse
comments: Facebook does not work comments feedback"
答案 4 :(得分:0)
不使用re:
删除无用的空间:
' '.join(text.split())
删除无用的\ n:
'\n'.join(filter(None, text.split('\n')))