之前发布过,但我做了一些编辑(这很可能是错误的)我的脚本观察任何包含想要销售的广告的帖子,有些人以"2.1k“(或类似的,1k,3.5k等)的形式发布他们的价格。
我的脚本读取消息,分离价格,并将其存储为“价格”,然后将其发送到不一致嵌入中。我遇到的问题是,它会看到一些消息,并使用字母"K“作为价格的单词。例如,一篇帖子说
"Buy from Truster seller (60+ Refs)
Lifetime - £4100 // $5300 Obo
Renewal -£3500 // $4500 Obo
US Wire/UK Bank Transfer, Transferwise, PayPal, BTC, Stripe, Rev
Discount for cash in UK发送的价格是“银行”字样
我以前从来没有使用过正则表达式,请让我知道我是否正确地使用了它,以及我可以做些什么来改变它。
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(" ")
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发布于 2020-10-21 11:15:31
您的split_message变量是一个列表,而不是字符串。您需要将正则表达式应用于列表中的每个字符串,如下所示:
for item in split_message:
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它可以处理你所看到的错误,并且应该可以成功地缓解它。在本例中,标签/spaces被搞乱了,所以不要只是复制和粘贴。
https://stackoverflow.com/questions/64456094
复制相似问题