简体   繁体   English

检查字符串是否为空(或空格)

[英]Check whether a string is empty (or spaces)

I have a string which is : 我有一个字符串是:

>>> a = "           "
>>> a.isspace()
False
>>> a
'\xe2\x80\x83\xe2\x80\x83             \xe2\x80\x83\xe2\x80\x83             \xe2\x80\x83\xe2\x80\x83             \xe2\x80\x83\xe2\x80\x83             \xe2\x80\x83\xe2\x80\x83             \xe2\x80\x83\xe2\x80\x83             \xe2\x80\x83\xe2\x80\x83             \xe2\x80\x83\xe2\x80\x83             \xe2\x80\x83\xe2\x80\x83             \xe2\x80\x83\xe2\x80\x83             \xe2\x80\x83\xe2\x80\x83             '
>>> print a
                                                                                                                                                                     
>>> 

As we can see, when I print string a, it is all spaces. 如我们所见,当我打印字符串a时,它是所有空格。 However, using isspace() cannot check it is a string full of spaces. 但是,使用isspace()无法检查它是否为包含空格的字符串。 How can I detect such kind of string to be a "space string"? 如何检测到这样的字符串是“空格字符串”?

You do not have a string containing only whitespace characters. 您没有仅包含空格字符的字符串。 You have a bytestring containing the UTF-8 encoding of a Unicode string containing only whitespace characters. 您有一个字节字符串,其中包含仅包含空格字符的Unicode字符串的UTF-8编码

Decoding the bytes in UTF-8 produces a Unicode string that reports True for isspace : 解码UTF-8中的字节会生成一个Unicode字符串,该字符串对isspace报告为True

>>> a.decode('utf-8').isspace()
True

but don't just slap decode('utf-8') into your code ad-hoc and hope it works. 但不要只是decode('utf-8')打入您的代码,并希望它能起作用。

Keep track of whether you're using Unicode or bytestrings at all times. 始终跟踪您是使用Unicode还是字节串。 Generally, work in Unicode, convert bytestring input to Unicode immediately, and only convert Unicode to bytestrings as it leaves your code. 通常,使用Unicode,立即将输入的字节串转换为Unicode,并且仅在代码离开代码时才将Unicode转换为字节串。

str.isspace() checks whether or not a string is only a space so it would not work if there are other characters present. str.isspace()检查字符串是否仅是空格,因此如果存在其他字符,则字符串将不起作用。

You can use str.contains(' ') to check if there are spaces in your string or 您可以使用str.contains('')检查字符串中是否有空格或

if ' ' in str:
    #do something
import re
if re.search(r"^\s+$"):
    print "All Spaces"

The regex above will match any string that contains only the following characters: 上面的正则表达式将匹配仅包含以下字符的任何字符串:

ASCII space , tab , line feed , carriage return , vertical tab , form feed ASCII spacetabline feedcarriage returnvertical tabform feed vertical tab


Alternatively, and probably more efficient, you can use strip() : 另外,也许更有效,您可以使用strip()

a = "             ".strip()
if not a:
    print "All spaces"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM