V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
aias
V2EX  ›  Python

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

  •  
  •   aias · 2016-05-17 13:40:55 +08:00 · 2754 次点击
    这是一个创建于 2893 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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?

    9 条回复    2016-05-17 14:44:27 +08:00
    fy
        1
    fy  
       2016-05-17 13:42:33 +08:00   ❤️ 1
    因为还有个 \n ,你把整个 results 都输出看看就知道了。

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

    话说你的头像也很有意思啊
    laoyur
        4
    laoyur  
       2016-05-17 13:45:08 +08:00
    一楼回复速度好快
    还有,我这个头像进了此楼感觉好另类
    aias
        5
    aias  
    OP
       2016-05-17 13:45:15 +08:00
    @tan9le 已解决,谢谢
    aias
        6
    aias  
    OP
       2016-05-17 13:45:33 +08:00
    @laoyur 异端
    darkmanno6
        7
    darkmanno6  
       2016-05-17 14:43:05 +08:00
    if result == "hello world\n":
    darkmanno6
        8
    darkmanno6  
       2016-05-17 14:43:45 +08:00
    if result == "hello world":
    darkmanno6
        9
    darkmanno6  
       2016-05-17 14:44:27 +08:00
    上一条错了,应该这样
    if result == "hello world\n":
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5181 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 01:22 · PVG 09:22 · LAX 18:22 · JFK 21:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.