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
chenqh
V2EX  ›  Python

关于 Python 的一个问题

  •  
  •   chenqh · 2019-02-26 12:15:23 +08:00 · 1955 次点击
    这是一个创建于 1857 天前的主题,其中的信息可能已经有所发展或是发生改变。
    a='ÿþU\x00S\x00E\x00 \x00[\x00P\x00h\x00o\x00n\x00e\x00D\x00a\x00t\x00a\x00]\x00\n'
    print(a)
    index=a.find('PhoneData')
    
    

    为什么 index==-1 呢?有点看不懂呀?
    有人指点下吗?

    第 1 条附言  ·  2019-02-26 14:00:18 +08:00
    还是我文件编码识别错了。。
    7 条回复    2019-02-26 14:13:04 +08:00
    40huo
        1
    40huo  
       2019-02-26 12:18:49 +08:00   ❤️ 1
    -1 就是没找到
    ysc3839
        2
    ysc3839  
       2019-02-26 12:51:35 +08:00   ❤️ 1
    你是对 find 有误解吗? find 寻找的是连续的整段字符串,中间不能包括别的字符的。
    Vegetable
        3
    Vegetable  
       2019-02-26 13:43:09 +08:00   ❤️ 1
    `\00`相当于 ascii 码的 NUL(null),空字符,虽然不可见但是他是存在的.
    所以你的 PhoneData 之间有别的字符.和你找的不一样.
    Vegetable
        4
    Vegetable  
       2019-02-26 13:46:40 +08:00   ❤️ 1
    RyougiShiki
        5
    RyougiShiki  
       2019-02-26 14:03:07 +08:00   ❤️ 1
    a=u'ÿþU\x00S\x00E\x00 \x00[\x00P\x00h\x00o\x00n\x00e\x00D\x00a\x00t\x00a\x00]\x00\n'
    print(a) # ÿþUSE [PhoneData]
    est
        6
    est  
       2019-02-26 14:09:46 +08:00   ❤️ 2
    a='ÿþU\x00S\x00E\x00 \x00[\x00P\x00h\x00o\x00n\x00e\x00D\x00a\x00t\x00a\x00]\x00\n'
    b=a.encode('latin1').decode('utf16', 'ignore')
    b.find('PhoneData')
    SleipniR
        7
    SleipniR  
       2019-02-26 14:13:04 +08:00   ❤️ 1
    In [29]: u"\x00P\x00h\x00o\x00n\x00e\x00D\x00a\x00t\x00a\x00" == u"PhoneData"
    Out[29]: False
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5317 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 09:33 · PVG 17:33 · LAX 02:33 · JFK 05:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.