Python 如果userinput字符串为空或数字或具有非ascii字符串
当用户输入名字时,如果它是空的,或者有数字或字母数字,或者有非ascii字符,我不会将它插入数据库 对于下面的代码,它不接受有效的输入,只有在我使用len和isDigit这两个条件时,它才能工作 而lenf_name==0或f_name.isdigit 或 f_name.encode'ascii',errors='ignore'或f_name.isalnum: 有人能解释一下如何解决这个问题吗?谢谢你抽出时间。代码的其余部分如下所示:Python 如果userinput字符串为空或数字或具有非ascii字符串,python,string,alphanumeric,Python,String,Alphanumeric,当用户输入名字时,如果它是空的,或者有数字或字母数字,或者有非ascii字符,我不会将它插入数据库 对于下面的代码,它不接受有效的输入,只有在我使用len和isDigit这两个条件时,它才能工作 而lenf_name==0或f_name.isdigit 或 f_name.encode'ascii',errors='ignore'或f_name.isalnum: 有人能解释一下如何解决这个问题吗?谢谢你抽出时间。代码的其余部分如下所示: import sqlite3 #connect a bui
import sqlite3
#connect a built in function to connect or create db
conn=sqlite3.connect('phonebook.db')
#Create a cursor function which allows us to do sql operations
crsr=conn.cursor()
#This function to check if table exists
def create_Table():
#Check if the table exists or not
crsr.execute("SELECT name FROM sqlite_master WHERE name='phonebook'")
tableSize=len(crsr.fetchall())#will be greater than 0 if table exists
if tableSize>0:
print()
else:
#create the table
crsr.execute(""" Create Table phonebook(
FirstName text NOT NULL,
LastName text,
Phone text PRIMARY KEY NOT NULL)
""")
#check if table got created or not
crsr.execute("SELECT name FROM sqlite_master WHERE name='phonebook'")
tableSize = len(crsr.fetchall()) # will be greater than 0 if table exists
if tableSize > 0:
print('Table was created successfully')
#This function will create new users and insert in DB
def create_User():
try:
while True:
rsp = input('Create new user: Y/N ?')
if rsp == 'y':
f_name = input('Enter first name: ')
# First name cannot be empty or have numeric values
while (len(f_name) == 0 or f_name.isdigit() or f_name.encode('ascii',errors='ignore') or f_name.isalnum()):
print('First name cannot be empty or have numeric values')
f_name = input('Enter first name: ')
l_name = input('Enter last name: ')
phone = input('Enter phone number: ')
crsr.execute("INSERT INTO phonebook VALUES (:FirstName, :LastName, :Phone)",
{'FirstName': f_name, 'LastName': l_name, 'Phone': phone})
conn.commit()
if rsp == 'n':
break
except:
print('UNIQUE constraint failed: phone number already exists')
用于确保字符串仅为字母:
f_name=输入“输入名字:”
如果f_name和f_name.isalpha:
你接受的逻辑在这里
此外,如果您需要检查这些字母是否为ASCII码,您可以轻松地将其编码长度与其自身进行比较:
f_name=输入“输入名字:”
如果f_name和f_name.isalpha和lenf_name==lenf_name.encode:
你接受的逻辑在这里
编辑添加的空字符串检查,即如果f_name如果您熟悉正则表达式,则可以通过以下方式测试条件不得为空且不得包含数字:
import re
# match one or more characters that range from a to z or A to Z
username_check = re.compile(r'[a-zA-Z]+')
...
while True:
if rsp == 'y':
f_name = input('Enter first name: ')
while not username_check.fullmatch(f_name):
print('First name cannot be empty or have numeric values')
f_name = input('Enter first name: ')
正则表达式的好处在于,您可以非常灵活地扩展当前的最小解决方案,以测试非常特定的模式:
import re
# allow unicode word characters
allowed = re.compile(r'\w+')
# numbers are still not allowed
forbidden = re.compile(r'\d')
while True:
f_name = input('Enter first name: ')
while not (allowed.fullmatch(f_name) and not forbidden.search(f_name)):
print('First name cannot be empty or have numeric values')
f_name = input('Enter first name: ')
你能给我们举个例子,说明哪个用户名目前确实被接受,但不应该被接受吗,Amy应该被接受,但不是2345或Am4y或Am$anda。谢谢条件:f_name.isalnum对于名称用户界面将为True。这似乎是相关的:如果OP不想使用整个unicode范围作为名称,出于什么原因,他应该可以扔掉其他合法名称。@Serge已经看到OPs的评论了。我可以为添加一个额外的条件,但我更喜欢等待声明完整的条件。大概也被接受了,谁知道呢。或者逗号本身不被接受,等等。必须澄清。讨论得很好,如果我没有正确澄清所有要求,很抱歉。这次讨论给了我更多的测试想法。
import re
# allow unicode word characters
allowed = re.compile(r'\w+')
# numbers are still not allowed
forbidden = re.compile(r'\d')
while True:
f_name = input('Enter first name: ')
while not (allowed.fullmatch(f_name) and not forbidden.search(f_name)):
print('First name cannot be empty or have numeric values')
f_name = input('Enter first name: ')