一个小脚本,为什么进不了这个判断?

2016-05-17 13:40:55 +08:00
 aias

test.py

import os,sys

os.system("echo \"================are you ok ? =====================\"")

def execCmd(cmd):
    r = os.popen(cmd)
    text = r.read()
    r.close()
    return text

action = sys.argv[1]

if action=="test":
        exeResult = os.popen("cat test1.txt")
        results = exeResult.readlines()
        exeResult.close()

        for result in results:
                print(result)
                if result == "hello world":
                        print("hello!")
        print("execute finally")


else:
        os.system("echo \">>>>>>>>>>>>>No Action Defind\"")


test1.txt 内容

this text from test1
my name is dd
hello world

输出:

================are you ok ? =====================
this text from test1

my name is dd

hello world

execute finally


为什么出不来那个 hello?

2760 次点击
所在节点    Python
9 条回复
fy
2016-05-17 13:42:33 +08:00
因为还有个 \n ,你把整个 results 都输出看看就知道了。

我承认,你头像的 doge 吸引了我
tan9le
2016-05-17 13:43:54 +08:00
if result == "hello world\n":
aias
2016-05-17 13:45:02 +08:00
@fy 谢咯!

话说你的头像也很有意思啊
laoyur
2016-05-17 13:45:08 +08:00
一楼回复速度好快
还有,我这个头像进了此楼感觉好另类
aias
2016-05-17 13:45:15 +08:00
@tan9le 已解决,谢谢
aias
2016-05-17 13:45:33 +08:00
@laoyur 异端
darkmanno6
2016-05-17 14:43:05 +08:00
if result == "hello world\n":
darkmanno6
2016-05-17 14:43:45 +08:00
if result == "hello world":
darkmanno6
2016-05-17 14:44:27 +08:00
上一条错了,应该这样
if result == "hello world\n":

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/279201

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX