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

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

  •  
  •   h19981126g · 2018-08-03 19:52:13 +08:00 · 12097 次点击
    这是一个创建于 2064 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2018-08-03 20:14:37 +08:00
    AX5N
        1
    AX5N  
       2018-08-03 20:00:58 +08:00   ❤️ 1
    re 被占用了
    kenorizon
        2
    kenorizon  
       2018-08-03 20:02:53 +08:00   ❤️ 1
    第 5 行给局部变量 re 赋值的时候把第二行 import 的 re 模块覆盖掉了
    所以第 6 行 re.findall 实际上尝试执行第 5 行的字符串的 findall 方法
    把正则字符串的变量换成别的名字就好
    TuringGunner
        3
    TuringGunner  
       2018-08-03 20:03:04 +08:00 via iPhone
    你变量名非要叫 re 么……
    wsh1108
        4
    wsh1108  
       2018-08-03 20:11:41 +08:00 via Android
    一般都叫 pattern 吧
    h19981126g
        5
    h19981126g  
    OP
       2018-08-03 20:14:37 +08:00
    @AX5N 好的吧
    @kenorizon
    @TuringGunner 知道了,谢谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1904 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:25 · PVG 00:25 · LAX 09:25 · JFK 12:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.