Python 如果消息内容后面有一个数字和一个字母,那么做些什么

Python 如果消息内容后面有一个数字和一个字母,那么做些什么,python,Python,我有一个脚本,刮一些转售服务器。有些人在价格中以2.1k的格式写下我的问题是我寻找字母“k”并在价格中使用它,但它寻找字母“k”的单词。例如,列表将显示“为我的产品购买2.1k”,因为字母“k”,它将显示价格为“购买”。我该如何确保它只查找后跟字母K的数字 check1 = False check2 = True for x in message.content.lower(): print(x) i

我有一个脚本,刮一些转售服务器。有些人在价格中以2.1k的格式写下我的问题是我寻找字母“k”并在价格中使用它,但它寻找字母“k”的单词。例如,列表将显示“为我的产品购买2.1k”,因为字母“k”,它将显示价格为“购买”。我该如何确保它只查找后跟字母K的数字

        check1 = False
        check2 = True
        for x in message.content.lower():
            print(x)
            if x == "k":
                check1 = True
            elif x == "$":
                check1 = True
            elif x == "£":
                check1 = True
        if check1 and check2: #both success so must be correct
            print("Is not a dm and includes a price")
            split_message = message.content.split(" ")
            price = None
            for x in split_message:
                if "$" in x:
                    #if "/" not in x:
                        price = x
                elif "£" in x:
                    #if "/" not in x:
                        price = x
                elif "k" in x:
                    #if "/" not in x:
                        price = x
更新:

if check1 and check2: #both success so must be correct
            print("Is not a dm and includes a price")
            split_message = message.content.split(" ")
            messageNoBold = message.content.replace('**','')
            price = None
            thePrice = re.findall("\d+(\.\d+)?[kK]", split_message)
            for x in thePrice:
                
                if "$" in x:
   
                        price = x
                        
                elif "£" in x:
                    
                        price = x
                        
                elif "k" in x:
                    
                        price = x
                        
            print(price)
返回-

return _compile(pattern, flags).findall(string)
TypeError: expected string or bytes-like object


您可以使用regex执行此操作:

re.findall("\d+(\.\d+)?[kK]", text)

如果您不想使用正则表达式,请使用Abion47解决方案:

your_str = "Taking $2.1k for my product"
if any([i.endswith('k') and not i[:-1].isalpha() for i in your_str.split()]):
    print("Is not a dm and includes a price")
    do_stuff_you_wanna_do_incase_its_a_price()

您可以尝试使用正则表达式
\d+(\.\d+)?[kK]
。我应该把它放在哪里?很抱歉,我对编码相当陌生。@Abion47您的正则表达式模式不起作用,例如标题“为我的产品获取2.1k”@Ahmet。@Abion47我更新了帖子。您能检查一下我是否做对了吗?我肯定这是错的。谢谢你,我应该把这个放在代码里的什么地方?我对python编码还比较陌生。@Lukemul69只需将“text”更改为message.content。此函数将返回所有出现的情况。我刚刚尝试了此功能,并认为它可以工作,直到“银行”一词作为价格出现。我更新了帖子。你能看到我是否正确执行了此功能吗?对不起,我将在脚本上放在哪里?我不太擅长蟒蛇,如果是原创的话。当且仅当字符串包含后跟“k”的数字价格时,它才会进入