from itertools import takewhile
check = lambda c: not c.isprintable() or c.isspace()
result = s[len(tuple(takewhile(check, s))): -len(tuple(takewhile(check, reversed(s))))]
# OR to not waste memory for tuple
result = s[sum(1 for _ in takewhile(check, s)): -sum(1 for _ in takewhile(check, reversed(s)))]
strip
从字符串的两端删除空格这里的最后一个字符不是空格字符。最后的第二个字符是空格
可能这不是一个好看的解决方案,但它将剥离所有不可打印和空白字符:
在这里,我使用^{} 从字符串开始获取所有不可打印(^{} )和空格(^{} )字符。然后使用^{} (或带有列表理解的选项中的^{} ),我得到了诸如字符的数量,并将该数量用作字符串切片中的开始索引。为了获得结束索引,我使用了相同的方法,但使用了反向(^{} )字符串
它不是一个普通的unicode空间。你可以这样取下它
相关问题 更多 >
编程相关推荐