如何在python中检查空白输入

如何在python中检查空白输入,python,file-io,Python,File Io,我对编程和python非常陌生。我正在编写脚本,如果客户键入空白,我想退出脚本。 问题是我怎么做才对? 这是我的尝试,但我认为是错误的 比如说 userType = raw_input('Please enter the phrase to look: ') userType = userType.strip() line = inf.readline() while (userType == raw_input) print "userType\n" if (userTyp

我对编程和python非常陌生。我正在编写脚本,如果客户键入空白,我想退出脚本。 问题是我怎么做才对? 这是我的尝试,但我认为是错误的

比如说

userType = raw_input('Please enter the phrase to look: ')
userType = userType.strip()

line = inf.readline()
while (userType == raw_input)
    print "userType\n"

    if (userType == "")
        print "invalid entry, the program will terminate"
        # some code to close the app

您提供的程序不是有效的python程序。因为您是初学者,所以对您的程序进行一些小的更改。这应该运行,并且做我理解的事情

这只是一个起点:结构不明确,你必须根据需要进行更改

userType = raw_input('Please enter the phrase to look: ')
userType = userType.strip()

#line = inf.readline() <-- never used??
while True:
    userType = raw_input()
    print("userType [%s]" % userType)

    if userType.isspace():
        print "invalid entry, the program will terminate"
        # some code to close the app
        break
userType=raw\u input('请输入要查看的短语:')
userType=userType.strip()

#line=inf.readline()您提供的程序不是有效的python程序。因为您是初学者,所以对您的程序进行一些小的更改。这应该运行,并且做我理解的事情

这只是一个起点:结构不明确,你必须根据需要进行更改

userType = raw_input('Please enter the phrase to look: ')
userType = userType.strip()

#line = inf.readline() <-- never used??
while True:
    userType = raw_input()
    print("userType [%s]" % userType)

    if userType.isspace():
        print "invalid entry, the program will terminate"
        # some code to close the app
        break
userType=raw\u input('请输入要查看的短语:')
userType=userType.strip()

#line=inf.readline()应用strip删除空白后,请改用此选项:

if not len(userType):
     # do something with userType
else:
     # nothing was entered

在应用strip删除空白后,请改用此选项:

if not len(userType):
     # do something with userType
else:
     # nothing was entered
您可以输入并检查是否还有剩余内容

import string

userType = raw_input('Please enter the phrase to look: ')
if not userType.translate(string.maketrans('',''),string.whitespace).strip():
      # proceed with your program
      # Your userType is unchanged.
else:
      # just whitespace, you could exit.
您可以输入并检查是否还有剩余内容

import string

userType = raw_input('Please enter the phrase to look: ')
if not userType.translate(string.maketrans('',''),string.whitespace).strip():
      # proceed with your program
      # Your userType is unchanged.
else:
      # just whitespace, you could exit.

我知道这已经很老了,但这可能对将来的人有所帮助。我想出了如何使用正则表达式来实现这一点。这是我的密码:

import re

command = raw_input("Enter command :")

if re.search(r'[\s]', command):
    print "No spaces please."
else:
    print "Do your thing!"

我知道这已经很老了,但这可能对将来的人有所帮助。我想出了如何使用正则表达式来实现这一点。这是我的密码:

import re

command = raw_input("Enter command :")

if re.search(r'[\s]', command):
    print "No spaces please."
else:
    print "Do your thing!"