{'\[\\n M a k i n g c o f f e e i s n o t r e l a t e d t o t h e c o n t e x t i n f o r m a t i o n p r o v i d e d .\]'}
我需要如下回复:
Making coffee is not related to the context information provided.
我试过如下:
import re
import json
import string
import re
my_int ={'\[\\n M a k i n g c o f f e e i s n o t r e l a t e d t o t h e c o n t e x t i n f o r m a t i o n p r o v i d e d .\]'}
a=\[\]
print(type(my_int))
result = re.sub(r'\[0-9\]', '\_', str(my_int))
print(type(result))
print(" ".join(result.split()))
print(result.translate({ord(c): None for c in string.whitespace}))
print(re.sub(r"\\s+", "", result), sep='')
print(re.sub(r"^\\s+", "", result), sep='')
print(re.sub(r"\\s+$", "", result), sep='')
print(re.sub(r"^\\s+|\\s+$", "", result), sep='')`
尝试了所有这些方法来得到结果,但没有成功。
注意:如您所见,所有字母都是单独的字符,因此我无法删除它们之间的空格。
帮助将不胜感激。
有人无缘无故地为您创建了一套。
他们在里面放了一个元素,那不是字符串,而是将字符串放在边
[
和 ]
字符中的一种丑陋方式。
不过你可以提取它。空格的软糖是暂时将双空格转换成其他东西,删除所有其他空格,然后带回双空格。只需确保您使用的序列(例如
!DOUBLESPACE!
)不能出现在您的字符串中。您可能想要使用您所在地区不使用的国际字符。
my_set ={'\[\\n M a k i n g c o f f e e i s n o t r e l a t e d t o t h e c o n t e x t i n f o r m a t i o n p r o v i d e d .\]'}
my_string = list(my_set)[0].replace("\[","").replace("\]","").replace("\\n","").replace(" ","!DOUBLESPACE!").replace(" ","").replace("!DOUBLESPACE!"," ")
print(my_string)
不需要
import
。
结果:
Making coffee is not related to the context information provided.
你绝对不想要一个
set
的结果。有人给了你一个集合作为输入。
上面代码的结果,
my_string
,只是一个字符串。确保使用那个,而不是集合,进行后续处理。
尝试:
import re
my_int = {'\[\\n M a k i n g c o f f e e i s n o t r e l a t e d t o t h e c o n t e x t i n f o r m a t i o n p r o v i d e d .\]'}
for s in my_int:
s = re.sub(r'\\.', '', s)
s = re.sub(r'\s(?=\S)|(?<=\S)\s', '', s)
print(s)
印花:
Making coffee is not related to the context information provided.