Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 python,我需要一种快速的方法来删除字符串中 \n 的所有实例。为了清楚起见,这是我想要的一个例子。
"I went \n to the store\n"
变成
"I went to the store"
我在想也许正则表达式是最好的方法。
使用str.replace:
str.replace
>>> "I went \n to the store\n".replace('\n', '') 'I went to the store'
对于相等的间距,您可以先使用拆分字符串str.split,然后使用str.join:
str.split
str.join
>>> ' '.join("I went \n to the store\n".split()) 'I went to the store'