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

Python 替换字符串的问题

  •  
  •   c00WKmdje2wZLrSI · 2018-09-28 11:36:08 +08:00 · 1357 次点击
    这是一个创建于 2029 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如这样一段字符串
    <meta name="detectify-verification" content="d0264f228155c7a1f72c3d91c17ce8fb" />
    <meta name="p:domain_verify" content="b87e3b55b409494aab88c1610b05a5f0"/>
    <meta name="alexaVerifyID" content="OFc8dmwZo7ttU4UCnDh1rKDtLlY" />
    <meta name="baidu-site-verification" content="D00WizvYyr" />

    content 里面的内容是随机的,如何把他们替换成
    <meta name="detectify-verification" content="d0264f228155c7a1f72c3d91c17ce8fb.html" />
    <meta name="p:domain_verify" content="b87e3b55b409494aab88c1610b05a5f0.html"/>
    <meta name="alexaVerifyID" content="OFc8dmwZo7ttU4UCnDh1rKDtLlY.html" />
    <meta name="baidu-site-verification" content="D00WizvYyr.html" />

    我只懂得把 content 的内容提取出来,再 for 循环一个个替换,但是感觉有点麻烦,有没有办法不使用循环,直接用 replace 或者 re 搞定?
    zhzer
        1
    zhzer  
       2018-09-28 11:58:14 +08:00 via Android
    过于基础了吧,建议百度,现在给你写了下次还要问
    imn1
        2
    imn1  
       2018-09-28 12:03:01 +08:00
    同意#1
    不就把
    " />
    替换成
    .html" />
    ?

    你不会从来没用过替换方法吧?
    luckybo
        3
    luckybo  
       2018-09-28 12:04:41 +08:00
    不就是 content=""里面的字符串+‘.html ’么😓
    c00WKmdje2wZLrSI
        4
    c00WKmdje2wZLrSI  
    OP
       2018-09-28 12:05:29 +08:00
    @zhzer
    @imn1
    不是,感觉我问的方式有问题,这种问法的确一替换就好了,我再想想怎么问好
    Raisu
        5
    Raisu  
       2018-09-28 12:05:30 +08:00
    这。。。。。。建议多做一些处理字符串的练习
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1393 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:48 · PVG 07:48 · LAX 16:48 · JFK 19:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.