Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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
Python3-Regex或其他检查变量中单词的方法?_Python - Fatal编程技术网

Python3-Regex或其他检查变量中单词的方法?

Python3-Regex或其他检查变量中单词的方法?,python,Python,这是我的代码: for post in socket("news", start=currentTime): if post["body"] == 'xyz' and post["author"] == 'admin' and post["url"] == 'latestnews': try: print ("Found it!") (...) 它正在工作,但仅当post[“body”]为“xyz”时才起作用。如果post[“body”]是“blabla-Bla-x

这是我的代码:

for post in socket("news", start=currentTime):
    if post["body"] == 'xyz' and post["author"] == 'admin' and post["url"] == 'latestnews':
        try: 
 print ("Found it!")
(...)
它正在工作,但仅当
post[“body”]
为“xyz”时才起作用。如果
post[“body”]
是“blabla-Bla-xyz”,则它不工作。如果我希望我的脚本显示“找到它!”,即使
post[“body”]
包含

“Lorem ipsum door sit amet,eum ne maiorum volatipat,ei have erat eruditi,在具有xyz情形模拟的情况下 mei,soleat laoreet assentior ea mel.mei assum的内容包括:, 这是一个圣所,它在这里工作。”


很抱歉,我是Python新手:-/

您可能只需在operator或str中使用即可

基本上,你会的

if(xyz in post["body"] ...) { 
// OR 
if(post["body"].find('xyz') != -1 and ... ) {
in操作符可能是最快的方法


.find将返回一个介于0和字符串长度-1之间的数字(以告诉您字符串的位置。但是,如果它找不到字符串,则返回-1。因此,如果它返回-1,则它不在那里,因此除-1之外的任何内容都表示它在那里。

您所要做的就是更改您的顺序。您可以使用运算符“in”检查字符串是否包含其他字符串

if 'xyz' in 'oiurwernjfndj xyz iekjerk':
    print True

你想要的是
而不是
==
@Jan:Morgan知道最好不要为一个常见的重复写一个完整的答案。:)谢谢@MorganThrapp:-)不,这是非常不习惯的。在
中使用
。是的,你是对的,我这么多年没有编写python代码,我已经习惯于做indexof操作。