我试图在Linux上删除python 2.7中的所有空格/制表符/换行符.
我写了这个,应该做的工作:
myString="I want to Remove all white \t spaces, new lines \n and tabs \t"
myString = myString.strip(' \n\t')
print myString
Run Code Online (Sandbox Code Playgroud)
输出:
I want to Remove all white spaces, new lines
and tabs
Run Code Online (Sandbox Code Playgroud)
这似乎是一件简单的事情,但我在这里缺少一些东西.我应该进口什么吗?
在Python中,如何列出POSIX扩展正则表达式匹配的所有字符[:space:]?
是否有一种编程方式来提取所涵盖的Unicode代码点[:space:]?
如果我在FireBug控制台中执行以下代码片段,它会以某种方式打印surprise!:
['surprise!', 'boring'][Number(0=="?")]
Run Code Online (Sandbox Code Playgroud)
但为什么?
UPD
对不起,伙计们,这是个玩笑!杰尔是第一个注意到的人!昨天我在一个字符串中找到了一个ZERO WIDTH SPACE,从那时起就有了一些乐趣=)
我需要计算 UTF-8 字符串中的单词数。即我需要编写一个Python函数,它需要“\xe0\xa4\x8f\xe0\xa4\x95\xe0\xa4\xac\xe0\xa4\xbe\xe0\xa4\xb0,\xe0\xa4\x8f\xe0 \xa4\x95\xe0\xa4\x95\xe0\xa5\x8c\xe0\xa4\x86、\xe0\xa4\xac\xe0\xa4\xb9\xe0\xa5\x81\xe0\xa4\xa4\xe0\ xa4\xaa\xe0\xa5\x8d\xe0\xa4\xaf\xe0\xa4\xbe\xe0\xa4\xb8\xe0\xa4\xbe, \xe0\xa4\xa5\xe0\xa4\xbe" 作为输入和返回 7(字数)。
\n\n我尝试了正则表达式“\\b”,如下所示。但结果不一致。
\n\nwordCntExp=re.compile(ur\'\\b\',re.UNICODE);\nsen=\'\xe0\xa4\x8f\xe0\xa4\x95 \xe0\xa4\xac\xe0\xa4\xbe\xe0\xa4\xb0,\xe0\xa4\x8f\xe0\xa4\x95 \xe0\xa4\x95\xe0\xa5\x8c\xe0\xa4\x86, \xe0\xa4\xac\xe0\xa4\xb9\xe0\xa5\x81\xe0\xa4\xa4 \xe0\xa4\xaa\xe0\xa5\x8d\xe0\xa4\xaf\xe0\xa4\xbe\xe0\xa4\xb8\xe0\xa4\xbe, \xe0\xa4\xa5\xe0\xa4\xbe\';\nprint len(wordCntExp.findall(sen.decode(\'utf-8\'))) >> 1;\n12 \nRun Code Online (Sandbox Code Playgroud)\n\n对上述答案的任何解释或解决上述问题的任何其他方法都值得赞赏。
\npython ×3
string ×2
hindi ×1
javascript ×1
python-2.7 ×1
regex ×1
strip ×1
unicode ×1
utf-8 ×1