我有下面的字符串,我正在努力找出避免它的最佳实践。
解决方案必须有一定的灵活性,因为我从一个API接收这个输入,我不能绝对确定当前的字符结构(\n
而不是\r
)将始终是相同的。
'"If it ain\'t broke, don\'t fix it." \nWent in for a detailed car wash.\nThe attendants raved-up my engine when taking the car into
the tunnel. NOTE: my car is...'
这个regex看起来应该可以工作:
text_excerpt = re.sub(r'[\s"\\]', ' ', raw_text_excerpt).strip()
我读到decode()
可能有用(而且通常是更好的解决方案)。
raw_text_excerpt.decode('string_unescape')
尝试了一些类似的方法,但没有成功。有什么建议吗?regex在这里最好吗?
您要查找的编解码器是
string-escape
:不过,我不确定他们是在什么版本中添加的。。。可能是你使用的旧版本,但没有。我在跑:
相关问题 更多 >
编程相关推荐