[英]Detecting whitespace in command line argument not working
I am trying to read in a command line argument to a python script and then detect if it has whitepaces or not.我正在尝试读取 python 脚本的命令行参数,然后检测它是否有空格。 I have this so far...到目前为止我有这个......
import sys
arg = sys.argv[1]
if arg.isspace() == True:
print ("Spaces detected")
But if I run the script with the following command then it does not detect that the argument has white spaces...但是,如果我使用以下命令运行脚本,那么它不会检测到参数有空格......
python myscript.py "my test agrument"
Can anyone see where I am going wrong?谁能看到我哪里出错了?
From what I understand you do not want to use isspace
but rather know if there are white space inside the string.据我了解,您不想使用isspace
而是想知道字符串中是否有空格。
Code not tested:未经测试的代码:
import sys
arg = sys.argv[1]
if " " in arg:
print ("Spaces detected")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.