Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 去除字符之间的空白并停止用户输入包含整数或标点符号_Python_Python 3.x_String_Methods_Python 3.8 - Fatal编程技术网

Python 去除字符之间的空白并停止用户输入包含整数或标点符号

Python 去除字符之间的空白并停止用户输入包含整数或标点符号,python,python-3.x,string,methods,python-3.8,Python,Python 3.x,String,Methods,Python 3.8,嗨,我可以在这篇文章的底部用一些指针来指示我的代码。(顺便说一句,我最近开始学习Python,因此非常感谢您的反馈,即使它与问题不完全相关) 因此,我的代码基本上需要做/考虑以下几点: 根据某人的名字和姓氏打印出其用户ID 用户ID总共不能超过8个字符 名字的前2个字符后接姓氏的最后6个字符 对于以下名称,一切都按我的要求进行: John Doe=“jodoe” 玛丽·安妮·理查森=“马尔森” 但当我们看到以下示例时,一切都会发生变化: J.K.Rowling=“J.owling” John D

嗨,我可以在这篇文章的底部用一些指针来指示我的代码。(顺便说一句,我最近开始学习Python,因此非常感谢您的反馈,即使它与问题不完全相关)

因此,我的代码基本上需要做/考虑以下几点:

  • 根据某人的名字和姓氏打印出其用户ID
  • 用户ID总共不能超过8个字符
  • 名字的前2个字符后接姓氏的最后6个字符
  • 对于以下名称,一切都按我的要求进行:

    John Doe=“jodoe”
    玛丽·安妮·理查森=“马尔森”

    但当我们看到以下示例时,一切都会发生变化:

    J.K.Rowling=“J.owling”
    John D O E=“jod O E”

    我不想允许使用标点符号和整数,我无法控制字符之间的空白。在某些情况下,名字将类似于“玛丽·安妮”,或者人们可能有多个姓氏,所以中间的空白肯定应该被允许作为用户输入,但我想要的是去掉它

    因此用户可以键入“Marie Anne Richardson”,这仍然会 允许用户“maardson”。然而:

    “John D O E”将导致“jodoe”。“约翰·多伊”会导致 “乔多伊”

    代码:

    可以使用.replace()函数。如果您只关心“.”和“.”,则可以使用以下几行:

    name = input(“Tell the name”)
    name1 = name.replace(".", "")
    name2 = name1.replace(“ “,””)
    
    可能使用.replace()函数。如果您只关心“.”和“.”,则可以使用这几行:

    name = input(“Tell the name”)
    name1 = name.replace(".", "")
    name2 = name1.replace(“ “,””)
    

    我建议您使用
    replace()
    函数

    你可以写:
    new\u first\u name=first\u name.替换(“.”,“”)

    要替换多个子字符串,应使用字典,如本例所示:

    我建议您使用
    replace()
    函数

    你可以写:
    new\u first\u name=first\u name.替换(“.”,“”)

    要替换多个子字符串,应使用字典,如本例所示:

    创建一个用于屏幕输入的函数通常是有意义的,例如:

    def get_input(query):
        while True:     #endless loop broken by return of acceptable input
            acceptable = True
            foo = input(query)
            for x in foo:
                if not(x.isalpha() or x.isspace()): #positive selection
                    print("No numbers or punctuation allowed")
                    acceptable = False
                    break
            if len(foo.split()[0]) < 2:    #first word length check
                print("First name must have at least two letters")
                acceptable = False
            if acceptable:   #break up string, erase all whitespace, insert
                             #one whitespace after first name, return
                return foo.split()[0] + ' ' + ''.join(foo.split()[1:])   
    
    def get_输入(查询):
    而True:#可接受输入的返回打破了无休止的循环
    可接受=正确
    foo=输入(查询)
    对于foo中的x:
    如果不是(x.isalpha()或x.isspace()):#正选择
    打印(“不允许使用数字或标点符号”)
    可接受=错误
    打破
    如果len(foo.split()[0])<2:#第一个字长检查
    打印(“名字必须至少有两个字母”)
    可接受=错误
    如果可以接受:#拆分字符串,删除所有空白,插入
    #名字后面有一个空格,返回
    返回foo.split()
    

    然后,对于每个输入,您可以简单地使用问题作为查询调用此函数。这保证了从函数返回的每个字符串在两个字母字符串之间只有一个空格。

    创建一个函数来筛选输入通常是有意义的,例如:

    def get_input(query):
        while True:     #endless loop broken by return of acceptable input
            acceptable = True
            foo = input(query)
            for x in foo:
                if not(x.isalpha() or x.isspace()): #positive selection
                    print("No numbers or punctuation allowed")
                    acceptable = False
                    break
            if len(foo.split()[0]) < 2:    #first word length check
                print("First name must have at least two letters")
                acceptable = False
            if acceptable:   #break up string, erase all whitespace, insert
                             #one whitespace after first name, return
                return foo.split()[0] + ' ' + ''.join(foo.split()[1:])   
    
    def get_输入(查询):
    而True:#可接受输入的返回打破了无休止的循环
    可接受=正确
    foo=输入(查询)
    对于foo中的x:
    如果不是(x.isalpha()或x.isspace()):#正选择
    打印(“不允许使用数字或标点符号”)
    可接受=错误
    打破
    如果len(foo.split()[0])<2:#第一个字长检查
    打印(“名字必须至少有两个字母”)
    可接受=错误
    如果可以接受:#拆分字符串,删除所有空白,插入
    #名字后面有一个空格,返回
    返回foo.split()
    

    然后,对于每个输入,您可以简单地使用问题作为查询调用此函数。这保证了从函数返回的每个字符串在两个字母字符串之间只有一个空格。

    对于干净的正则表达式解决方案,我会:

    重新导入
    样本=[“约翰·多伊”、“J.K.罗琳”、“玛丽·安妮·理查森”、“约翰·多伊”、“约翰·多伊”]
    res=“”
    对于样品中的el:
    打印(f“在{el}之前”)
    res=re.sub(r“[^a-z\s]”,“”,el.lower().strip())
    
    res=re.sub(r“(?对于干净的正则表达式溶液-我会:

    重新导入
    样本=[“约翰·多伊”、“J.K.罗琳”、“玛丽·安妮·理查森”、“约翰·多伊”、“约翰·多伊”]
    res=“”
    对于样品中的el:
    打印(f“在{el}之前”)
    res=re.sub(r“[^a-z\s]”,“”,el.lower().strip())
    
    res=re.sub(r)(?您可以尝试删除标点符号,如以下链接所示,然后将空格替换为.replace(“,”)
    J.K.Rowling
    应该生成什么?也许你想在尝试处理字符串之前进行某种用户输入筛选?谢谢!替换函数肯定比我的好,但它不会阻止用户错误或故意键入其他符号。我认为创建一个如中微子所示的函数_下面的逻辑就是解决这个问题的方法。您可以尝试删除标点符号,如下面的链接所示,然后将空格替换为.replace(“,”)
    J.K.Rowling
    应该生成什么?也许你想在尝试处理字符串之前进行某种用户输入筛选?谢谢!替换函数肯定比我的好,但它不会阻止用户错误或故意键入其他符号。我认为创建一个如中微子所示的函数_下面的逻辑将是实现这一点的方法。感谢这一点,我尝试了它,它似乎是有效的,除了名字:John和姓氏:Doe Doe我还没有学会如何创建自己的函数,但我得到了这段代码来运行并给出