推荐学习书目
› 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
Topmax
V2EX  ›  Python

Python 正则表达式 re 中出现'str' object has no attribute 'findall'

  •  
  •   Topmax · Aug 3, 2018 · 13192 views
    This topic created in 3013 days ago, the information mentioned may be changed or developed.
    Python 正则表达式 re 中出现'str' object has no attribute 'findall',尝试了很多次,就是不行!
    无论是 findall 还是 match 还是 search 都是'str'不行,.text 和.content 也都试过,总是报错。麻烦大神们帮个忙!
    代码如下

    import requests
    import re
    proxies = {"http": "http://222.190.126.62:808"}
    r = requests.get('http://icanhazip.com/', proxies=proxies).text
    re = '^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$'
    ip = re.findall(re, r)
    print(ip)
    5 replies  •  2018-08-03 20:14:37 +08:00
    AX5N
    AX5N
    Aug 3, 2018❤️1
    1
    re 被占用了
    kenorizon
    kenorizon
    Aug 3, 2018❤️1
    2
    第 5 行给局部变量 re 赋值的时候把第二行 import 的 re 模块覆盖掉了
    所以第 6 行 re.findall 实际上尝试执行第 5 行的字符串的 findall 方法
    把正则字符串的变量换成别的名字就好
    TuringGooner
    TuringGooner
    Aug 3, 2018 via iPhone
    3
    你变量名非要叫 re 么……
    wsh1108
    wsh1108
    Aug 3, 2018 via Android
    4
    一般都叫 pattern 吧
    Topmax
    Topmax
    OP
    Aug 3, 2018
    5
    @AX5N 好的吧
    @kenorizon
    @TuringGunner 知道了,谢谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1517 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 23:16 · PVG 07:16 · LAX 15:16 · JFK 18:16
    ♥ Do have faith in what you're doing.